gpt4 book ai didi

json - 如何将 JSON 文件导入 Angular 库?

转载 作者:太空狗 更新时间:2023-10-29 18:09:22 25 4
gpt4 key购买 nike

我在将 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/

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