作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在将 JSON 文件导入 Angular 7 库中的环境文件时遇到问题。我的 environment.ts
文件如下所示:
import firebase from './firebase.json';
export const environment = {
production: false,
firebase,
};
和firebase.json
:
{
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"projectId": "",
"storageBucket": "",
"messagingSenderId": ""
}
但不幸的是,当运行 ng build
时它失败了:
> sdk@0.0.0 build <path-to-project>/sdk
> ng build sdk
Building Angular Package
Building entry point 'sdk'
Compiling TypeScript sources through ngc
Bundling to FESM2015
BUILD ERROR
Unexpected token / in JSON at position 0
SyntaxError: Unexpected token / in JSON at position 0
at JSON.parse (<anonymous>)
at Object.transform (<path-to-project>/sdk/node_modules/rollup-plugin-json/dist/rollup-plugin-json.cjs.js:18:20)
at <path-to-project>/sdk/node_modules/rollup/dist/rollup.js:20962:25
Unexpected token / in JSON at position 0
SyntaxError: Unexpected token / in JSON at position 0
at JSON.parse (<anonymous>)
at Object.transform (<path-to-project>/sdk/node_modules/rollup-plugin-json/dist/rollup-plugin-json.cjs.js:18:20)
at <path-to-project>/sdk/node_modules/rollup/dist/rollup.js:20962:25
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sdk@0.0.0 build: `ng build sdk`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the sdk@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! xxx/.npm/_logs/2019-04-10T13_40_47_486Z-debug.log
我已经试过了:
1) 添加到 tsconfig.json
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
2) 添加 typings.d.ts
和
declare module '*.json' {
const value: any;
export default value;
}
3) 将 import
更改为 require
但到目前为止还没有运气。
我的 devDependencies 包括:
"@angular-devkit/build-ng-packagr": "^0.13.8",
"@angular/cli": "~7.3.6",
"@angular/common": "~7.2.0",
"@angular/compiler": "^7.2.12",
"@angular/compiler-cli": "^7.2.12",
...
最佳答案
在经历了很多痛苦之后,我们找到了解决方案 here .
这基本上是您在第一次尝试时所做的,但是您需要再添加一个选项。
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
另外:
"annotateForClosureCompiler": false
注意:对于最后一个 Prop ,这是同一个文件,但不是将其添加到 compilerOptions
,而是将其添加到 angularCompilerOptions
关于json - 如何将 JSON 文件导入 Angular 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55615003/
我是一名优秀的程序员,十分优秀!