【问题标题】:.json file not recognised using http.get?.json 文件无法使用 http.get 识别?
【发布时间】:2018-06-11 11:07:15
【问题描述】:

我正在尝试访问存储在 .json 文件中的输入数据,但由于某种原因无法识别我的路径:

此方法在请求数据时运行:

    getData() {
  console.log('this is working');
  this.http.get('src/app/inputs/inputs.json')
  .subscribe(
    (data: any) => {
      console.log('this is working');
      console.log(data.text());
     }
   );
  }
}

这是我的文件夹结构:

只要单击单独的按钮,我就可以添加数据,只是检索它是我卡住的地方。

【问题讨论】:

标签: angular


【解决方案1】:

我认为您必须在 angular.json 的资产部分添加您的输入文件夹 例如

"assets": [
  "src/assets",
  "src/inputs",
  "src/favicon.ico"
]

然后通过

访问您的数据
this.http.get('inputs/inputs.json')

【讨论】:

  • 我要求将.json 移至assets,因为它将成为静态资产;)+1
【解决方案2】:

试试this.http.get('./inputs/inputs.json')

【讨论】:

  • 哪个文件包含这个函数?
  • app.component.ts
  • 如果你使用 angular-cli,你必须把你的 file.json 放在 assets/文件夹中。 Angular-cli 清理 index.html、style.css 和几个 .js 并复制文件夹 assets 中的所有文件
猜你喜欢
  • 2018-06-25
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2014-09-30
  • 2017-07-20
  • 2023-04-08
相关资源
最近更新 更多