Loading local JSON file

前端 未结 23 1607
悲哀的现实
悲哀的现实 2020-11-22 01:28

I\'m trying to load a local JSON file but it won\'t work. Here is my JavaScript code (using jQuery):

var json = $.getJSON("test.json");
var data = e         


        
23条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 01:50

    In TypeScript you can use import to load local JSON files. For example loading a font.json:

    import * as fontJson from '../../public/fonts/font_name.json';
    

    This requires a tsconfig flag --resolveJsonModule:

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

    For more information see the release notes of typescript: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html

提交回复
热议问题