我有一个导入到组件(app.component.ts)中的json文件
import data_json from '../assets/json/general.json';
但是当我尝试使用以下命令生成生产版本时,出现错误:
ng build prod
这是错误:
ERROR in src/app/app.component.ts(12,23): error TS2307: Cannot find module '../assets/json/general.json'.
如果我启动ng serve,它会在控制台中警告我,但不会出现错误,我会访问浏览器,并且看到json正确加载
使用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)
这是我的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"
}
}
}
}
我该如何解决?
答案 0 :(得分:1)
如果您不希望在运行时之前解析该文件,则需要通过HTTP获取JSON文件。但是我想那没有多大意义。
有关如何加载JSON的更多信息,请参见此: