简体   繁体   English

如何使用axios将json文件导入角度组件?

[英]How can I import a json file into an angular component with axios?

I generated(ng-cli) an angular 5 app and created this component: 我生成了(ng-cli)一个有角度的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);
      });
  }
}

I am trying to import my json data into the component with axios. 我正在尝试使用axios将json数据导入组件。 When I run npm start I get this error: 当我运行npm start时,出现以下错误:

GET http://localhost:4200/data.json 404 (Not Found) GET http:// localhost:4200 / data.json 404(未找到)

How can I import my json without issues? 我怎样才能毫无问题地导入json?

Here is a link to github 这是指向github的链接

您可以将json放在资产文件夹中,也可以修改angular-cli.json并将json包含在资产下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM