在Angular 9中,可以通过使用HttpClient模块来获取JSON对象的值。以下是一个示例代码:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule
],
...
})
export class AppModule { }
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
getData() {
this.http.get('assets/data.json').subscribe((data) => {
console.log(data);
// 在这里处理获取到的JSON数据
});
}
ngOnInit() {
this.getData();
}
请确保将上述代码中的“assets/data.json”替换为你实际的JSON文件路径。
在这个示例中,我们使用HttpClient的get()方法来获取JSON文件的内容,并通过subscribe()方法来订阅获取到的数据。你可以在subscribe()方法的回调函数中处理获取到的JSON数据。
希望这个示例可以帮助你访问JSON对象的值。