我正在通过将具有特定语言的json文件传递到网页来在我的应用程序中设置翻译服务。在完成所有连接之后,我想进行导入并进行测试,并在控制台日志中显示提取的数据。但是毕竟控制台日志显示出一个带有404找不到错误的空对象
我检查了所有路径,导入,连接,还向angular.json中的'assets'属性添加了路径(尝试重新启动ng serve),但是错误仍然存在。
export class TranslateService {
data:any = {};
constructor(private http: HttpClient) { }
use(lang: string): Promise<{}> {
return new Promise<{}>((resolve, reject) => {
const langPath = `assets/${lang}.json`;
this.http.get<{}>(langPath).subscribe(
translation => {
this.data = Object.assign({}, translation, {});
resolve(this.data);
},
error => {
this.data = {};
resolve(this.data);
}
);
});
}
import { TranslateService } from './translate.service';
export class AppComponent {
constructor(private router: Router, private meta: Meta, private cd: ChangeDetectorRef, private translate: TranslateService) {
translate.use('kz').then(() => {
console.log(translate.data);
});
}
我希望对象内部有数据,但会变空。
文件位置:
src
-app
--translate.service.ts
--app.component.ts
-assets
--kz.json
angular.json:
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": ["src/favicon.ico", "src/assets"],
"styles": ["src/styles/styles.scss"],
"stylePreprocessorOptions": {
"includePaths": ["src/styles"]
},
"scripts": []
}