简体   繁体   English

Angular 6-json文件无法针对生产环境进行编译

[英]Angular 6 - The json file does not compile for to production enviroment

I have a json file imported into my component (app.component.ts) 我有一个导入到组件中的json文件(app.component.ts)

import data_json from '../assets/json/general.json';

But when I try to generate the production version using the following command I get an error: 但是,当我尝试使用以下命令生成生产版本时,出现错误:

ng build prod

This is the error: 这是错误:

ERROR in src/app/app.component.ts(12,23): error TS2307: Cannot find module '../assets/json/general.json'.

If I launch ng serve, it gives me a warning in the console, but it does not give me an error, I access the browser and I see the json loaded correctly 如果我启动ng serve,它会在控制台中向我发出警告,但不会给我一个错误,我会访问浏览器,并且看到json正确加载

With HTTP, ng build --prod compile fine, but my browser console shows the next error: 使用HTTP时,ng build --prod可以正常编译,但是我的浏览器控制台显示下一个错误:

core.js:1521 ERROR TypeError: Cannot read property 'menu' of undefined
    at Object.eval [as updateDirectives] (AppComponent.ngfactory.js:414)
    at Object.updateDirectives (core.js:10494)
    at checkAndUpdateView (core.js:10147)
    at callViewAction (core.js:10388)
    at execComponentViewsAction (core.js:10330)
    at Object.checkAndUpdateView (core.js:10153)
    at ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges (core.js:8534)
    at core.js:4411
    at Array.forEach (<anonymous>)
    at ApplicationRef.push../node_modules/@angular/core/fesm5/core.js.ApplicationRef.tick (core.js:4411)

This is my json file (general.json) 这是我的json文件(general.json)

{
  "imgsesion": "fa_closesesion.png",
  "texthome": "volver a la home",
  "logo": "fa_logo.png",
  "contact": "fainfo@gmail.com",
  "menu": {
    "background": "orange",
    "link1": "ESCRITOR",
    "link2": "MÚSICO",
    "link3": "AYUDA ADMIN",
    "submenu": {
      "link1": {
        "text1": "novelas",
        "text2": "obras de teatro"
      },
      "link2": {
        "text1": "compositor",
        "text2": "intérprete"
      }
    }
  }
}

How can I solve it? 我该如何解决?

You need to fetch the JSON file via HTTP if you don't want to parse the file until runtime. 如果您不希望在运行时之前解析该文件,则需要通过HTTP获取JSON文件。 But I guess that doesn't make much sense. 但是我想那没有多大意义。

See this for more information on how to load JSON: 请参阅此以获取有关如何加载JSON的更多信息:

https://stackoverflow.com/a/39410788/4521733 https://stackoverflow.com/a/39410788/4521733

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

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