简体   繁体   English

从资产导入 Json 文件 - Angular Production Build 问题

[英]Import Json file from asset - Angular Production Build Issue

I am importing my json file from asset folder to read some configurations inside the app which works perfectly and I can change the json values based on my requirement我正在从资产文件夹导入我的 json 文件以读取应用程序内部的一些配置,该配置完美运行,我可以根据我的要求更改 json 值

you can refer : https://www.techiediaries.com/import-local-json-files-in-typescript/你可以参考: https : //www.techiediaries.com/import-local-json-files-in-typescript/

But in the production build version, When try to change the values from json file in the asset folder, it reads development data但是在生产构建版本中,当尝试从资产文件夹中的 json 文件更改值时,它会读取开发数据

after the investigation we found that json data are embedded in main-es5 file,经过排查我们发现main-es5文件中嵌入了json数据,

is there is any way to read json file using import method and we can change data dynamically (based on environment)有什么方法可以使用导入方法读取json文件,我们可以动态更改数据(基于环境)

I would suggest the cleaner way to load it via HttpClient , and if it contains some configuration, I think this is the way you can change the data in assets and app will load again and again with HttpClient.我建议通过HttpClient以更简洁的方式加载它,如果它包含一些配置,我认为这是您可以更改资产中数据的方式,并且应用程序将使用 HttpClient 一次又一次地加载。

And if you want to read based on the environment, typescript import doesn't work.如果你想根据环境阅读,打字稿导入不起作用。

Create a service, read the JSON via HttpClient based on pre-placed environment-specific JSON.创建一个服务,基于预先放置的特定于环境的 JSON 通过 HttpClient 读取 JSON。

This is another alternative solution you can try.这是您可以尝试的另一种替代解决方案。

  1. Create one app.constant.ts file创建一个app.constant.ts文件
    //
    // ===== File app.constants.ts    
    //
    'use strict';
    export const appConstants = {

       'key':'value',
       'key1':'value'
    };
  1. Import like this in component :在组件中像这样导入:
    import {appConstants} from './../app.constants';
  1. Use in component like this :在这样的组件中使用:

    this.variable = appConstants.key;

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

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