【问题标题】:Angular 5 ReferenceError: require is not defined and TypeError: stream is undefined after implements a method to import JSONAngular 5 ReferenceError: require is not defined and TypeError: stream is undefined after implementation a method to import JSON
【发布时间】:2018-09-07 11:42:30
【问题描述】:

我是 Angular(5) 的新手,我尝试将 JSON 文档从我的资产导入组件。
此 JSON 文档包含我的表单中一些选择的信息,我将在此组件中展示。
但实际上,在我实现了这个方法之后:

private getJson(url: string, http: HttpClient) {
    if (!url.search("[^]*.json")) {
      throw InvalidPathException;
    } else {
      return http.get(url);
    }
}

并订阅此,
我在浏览器中看到错误:

ReferenceError: 要求未定义

TypeError: 流未定义

有谁知道这个问题以及如何解决它?

【问题讨论】:

  • 您的错误看起来不像来自此代码 sn-p。
  • 你为什么要在这个里面传HttpClient?

标签: javascript angular typescript angular5


【解决方案1】:

无法识别给定代码中的错误。但我可以与您分享另一种在 angular5 代码中处理资产文件中的 JSON 文档的方法。您可以根据需要更改 JSON 文件的以下名称和内容。也许它会帮助你:

在打字稿文件中:

ngOnInit() {


this.http.get('assets/json/brandicon.json')
  .toPromise()
  .then(res => res.json().icons)
  .then(brandIcon => this.brandIcon = brandIcon);}

并在 src\assets\json\brandicon.json

{
"icons": [
    {
        "icon": "fa fa-500px"
    },
    {
        "icon": "fa fa-amazon"
    }]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2018-01-02
    • 2015-01-29
    • 2016-05-08
    • 2019-09-29
    • 2022-12-01
    • 1970-01-01
    相关资源
    最近更新 更多