gpt4 book ai didi

json - ( typescript )加载本地 json 文件时抛出意外 token

转载 作者:搜寻专家 更新时间:2023-10-30 21:14:47 31 4
gpt4 key购买 nike

我有一个用 json 编写的配置文件,例如:

{
"example": "example"
}

我在 typescript 中阅读了这个文件,如:

var events = require('json!./example_file.json');

最后我得到了意想不到的 token :

index.html:18 Error: (SystemJS) Unexpected token :
example_file.json:2
"example": "example"
^
SyntaxError: Unexpected token :
at Object.exports.runInThisContext (vm.js:76:16)

当我从 key 中删除双引号时,脚本将正确运行。但是如何读取标准 JSON 格式的 JSON 文件呢?我不想删除引号。

谢谢!

最佳答案

这里有一篇关于此的文章:https://hackernoon.com/import-json-into-typescript-8d465beded79只需添加

declare module "*.json" {
const value: any;
export default value;
}

到您的 typings.d.ts 配置文件并使用 import 读取您的 json 文件

关于json - ( typescript )加载本地 json 文件时抛出意外 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372915/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com