如何使用 axios 将 json 文件导入到 angular 组件中?
How can I import a json file into an angular component with axios?
我生成了 (ng-cli) 一个 angular 5 应用程序并创建了这个组件:
import {Component} from '@angular/core';
import axios from 'axios';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
axios.get('./data.json')
.then((response) => {
console.log(response.data);
});
}
}
我正在尝试将我的 json 数据导入到带有 axios 的组件中。
当我 运行 npm start 我得到这个错误:
GET http://localhost:4200/data.json 404 (Not Found)
如何才能顺利导入 json?
这里是link到github
要么将 json 放入资产文件夹,要么修改 angular-cli.json 并将 json 包含在资产下。
我生成了 (ng-cli) 一个 angular 5 应用程序并创建了这个组件:
import {Component} from '@angular/core';
import axios from 'axios';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
axios.get('./data.json')
.then((response) => {
console.log(response.data);
});
}
}
我正在尝试将我的 json 数据导入到带有 axios 的组件中。 当我 运行 npm start 我得到这个错误:
GET http://localhost:4200/data.json 404 (Not Found)
如何才能顺利导入 json?
这里是link到github
要么将 json 放入资产文件夹,要么修改 angular-cli.json 并将 json 包含在资产下。