我正在为angular开发一个国际化库,我想做一个angular组件,它返回给定语言的所有翻译。只是为了测试,我创建了以下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,如果我从浏览器访问它,一切都会正常工作(我有以下结果:" this "," is ","Sparta“显示。问题是,我想用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>
我不知道我错过了什么?它是我必须添加到请求中的标头吗?
需要说明的是,我实际上使用的是angular 7,我使用命令"npm run start“启动服务器,基本上就是执行"ng serve”命令。
发布于 2019-02-03 12:58:36
Translate和Test是两个组件。
希望这就是你提到的意思
https://stackoverflow.com/questions/54499624
复制相似问题