Get a local json file on NativeScript

后端 未结 4 809
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 07:29

How to get a local big json data?

I have tried this, but I had no success:

var sa = require(\"./shared/resources/sa.json\");
var array = new observab         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 08:16

    As of TypeScript version 2.9.x and above (in NativeScript 5.x.x is using versions 3.1.1 and above) we can now use resovleJsonModule option for tsconfig.json. With this option, the JSON files can now be imported just as modules and the code is simpler to use, read and maintain.

    For example, we can do:

    import config from "./config.json";
    
    console.log(config.count); // 42
    console.log(config.env); // "debug"
    

    All we need to do is to use TypeScript 2.9.x and above and enable the propety in tsconfig.json

    // tsconfig.json
    {
        "compilerOptions": {
            "module": "commonjs",
            "resolveJsonModule": true,
            "esModuleInterop": true
        }
    }
    

    A sample project demonstrating the above can be found here

提交回复
热议问题