【发布时间】:2018-12-21 03:24:47
【问题描述】:
如果你想帮助我检查发生了什么,这里是 Github 中的存储库:https://github.com/sandrocsimas/ngx-material-palette-picker
我正在尝试创建我的第一个角度库。 我使用 angular cli 命令生成了代码:
ng new ngx-material-palette-picker-app
ng generate library ngx-material-palette-picker
在组件NgxMaterialPalettePickerComponent我正在导入一个json文件,但是构建抛出以下错误:
projects/ngx-material-palette-picker/src/lib/ngx-material-palette-picker.component.ts(2,31): error TS2307: Cannot find module './palettes.json'.
这是我的组件:
import {Component, OnInit} from '@angular/core';
import * as palettesJSON from './palettes.json';
@Component({
selector: 'ngx-mpp',
template: `,
styles: []
})
export class NgxMaterialPalettePickerComponent implements OnInit {
constructor() {
}
}
我在根路径下创建了一个名为types的目录,并将以下代码添加到index.d.ts:
declare module '*.json' {
const value: any;
export default value;
}
我还在主 tsconfig.json 中添加了目录 types 到 typeRoots。
"typeRoots": [
"node_modules/@types",
"types"
],
该项目在 Sublime 编辑器中没有显示任何错误,但是当我尝试使用命令 ng build --prod ngx-material-palette-picker 构建项目时,我收到了此问题开头所述的错误。如何解决这个问题?
【问题讨论】:
-
您解决了吗?我有同样的问题。
-
@RossRawlins,不,我无法解决。
-
所以你不能从本地目录导入json文件是假的
标签: angular angular-cli angular-cli-v6