Ajax调用的角应用返回上的现有路线错误404

问题描述 投票:0回答:1

我工作的一个国际化库的角度,我想打它返回给定语言的所有翻译的角度成分。只是为了测试我创建了以下TranslateComponent

import {Component} from '@angular/core';

@Component({
    selector: 'trans-translate',
    templateUrl: './translate.component.html'
})
export class TranslateComponent {
    // region Public fields
    public data: string[] = ['This', 'is', 'Sparta'];
    // endregion
}

与显示数据的简单的HTML模板:

{{ data | json }}

其访问该组件的URL是http://localhost:4200/translate,如果我从浏览器evertything工作访问它很好(我有以下结果:[“这”,“是”,“斯巴达”]显示的问题是,我想打电话给。它与HttpClient的,有它不工作,我只是做这个简单的测试:

import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Component({
    selector: 'app-page-test',
    templateUrl: './test-page.component.html'
})
export class TestPageComponent implements OnInit {
    // region Constructor
    public constructor(
        private readonly _HTTP: HttpClient
    ) {}
    // endregion

    // region Public methods
    public ngOnInit(): void {
        this._HTTP.get('/translate').subscribe((result: any): void => {
            console.log(result);
        });
    }
    // endregion
}

但我得到的是与身体的错误404:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /translate</pre>
</body>
</html>

我想不出我缺少什么?它是一个头我要补充的要求吗?

为了记录我实际使用的角度7和我开始使用命令“故宫运行启动”,这basicaly只执行TU“NG服务”命令的服务器。

angular http request localhost
1个回答
0
投票

翻译和测试两个组成部分。本地主机:4200 /翻译其实是一个部件,并显示该数据。

而在测试页面,您试图调用使用服务的HttpClient。它不会调用转换组件,并返回数据。

希望这是你的意思是,当你提到的

T的网页本地主机:4200 /测试发出请求到localhost:4200 /翻译,所以我为什么会是问题

© www.soinside.com 2019 - 2024. All rights reserved.