简体   繁体   English

Angular2:如何使已编译(捆绑)的项目从JSON文件中获取数据?

[英]Angular2: How to make compiled (bundled) project to take data from JSON file?

In my Angular2 project I take settings from JSON file and I would like it to take settings from JSON after compiling it for production. 在我的Angular2项目中,我从JSON文件中获取设置,我希望它在编译用于生产后从JSON中获取设置。

I compiled the project with ng build command. 我用ng build命令编译了项目。 And now I see that angular-cli inserted content of the JSON file into main.bundle.js . 现在,我看到angular-cli将JSON文件的内容插入到main.bundle.js So the compiled project does not take data from JSON. 因此,编译后的项目不会从JSON中获取数据。

How to compile an Angular2 project so that it will take data from JSON file? 如何编译一个Angular2项目,以便它将从JSON文件中获取数据?

update Here is a code of using JSON in the project: 更新以下是在项目中使用JSON的代码:

export const CONFIG = require('../../assets/config/index.json');

How do you use your JSON ? 您如何使用JSON? (please show some code). (请显示一些代码)。

If you import it, it's normal that your JSON is bundled. 如果导入它,则将JSON捆绑在一起是正常的。 If you want to use it as an external file so you can modify it on the fly and just reload the page with the new data, you should treat that JSON file as a simple web request and use Http to get it. 如果要将其用作外部文件,以便可以随时对其进行修改,并仅使用新数据重新加载页面,则应将该JSON文件视为简单的Web请求,并使用Http进行get This way it won't be bundled. 这样就不会捆绑了。

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

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