(typescript) 在載入本地 json 文件時拋出意外的標記
我有一個使用 JSON 編寫的配置文件,如下:
{
"example": "example"
}
我在 TypeScript 中這樣讀取這個檔案:
var events = require('json!./example_file.json');
最後我得到了意外的標記:
index.html:18 Error: (SystemJS) Unexpected token :
example_file.json:2
"example": "example"
^
SyntaxError: Unexpected token :
at Object.exports.runInThisContext (vm.js:76:16)
當我從鍵中移除雙引號時,腳本就能正確運行。但我該如何以標準 JSON 格式讀取 JSON 文件呢?我不想移除引號。
謝謝!
答案#1
這裡有一篇關於那個的文章:https://hackernoon.com/import-json-into-typescript-8d465beded79 只要添加
declare module "*.json" {
const value: any;
export default value;
}
到你的 typings.d.ts 配置文件,並使用 import 來讀取你的 json 文件
