无法将JSON数据访问到angular(.ts)文件中 [英] Not able to access JSON data into angular(.ts) file

查看:321
本文介绍了无法将JSON数据访问到angular(.ts)文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ngx-translate为我的Web应用程序实现国际化.我能够访问HTMl文件中的JSON数据,但不能访问angular(.ts)文件中的JSON数据.谁能建议我如何在.ts文件中访问它.

I am trying to implement internationalization for my web application by using ngx-translate . I am able to access JSON data in the HTMl file but not able to access JSON data in the angular(.ts) file. Can anyone suggest me how to access it in .ts file.

推荐答案

就像调用后端服务一样,您可以从json文件中获取输入.语法相同,但是如果您从本地json文件读取,则它是一个同步调用.

You get the input from json files like you call a backend service. The syntax is the same, but if you read from a local json file it is an synchronous call.

// Import HTTP Client
import { HttpClient } from '@angular/common/http'; 

// Define property
myDataObject: any;

// Dependency Injection
constructor(private http: HttpClient) {}

// Get the data
ngOnInit() {   
  this.myDataObject = this.http.get<any>("assets/json/data.json")
    .map(response => response)
}

现在,您可以从json对象获取属性,例如:

Now you can get the properties from you json object like:

this.myDataObject.myProperty

这篇关于无法将JSON数据访问到angular(.ts)文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆