【发布时间】:2017-01-14 08:03:57
【问题描述】:
我正在考虑使用this Yeoman generator 作为一个包含一些我可以发布的可重用表单组件的迷你项目的启动器。生成器构建一个模块和一个示例组件、指令、管道和服务配置为使用 (you can see the template files for the generator here)。
然后我使用npm link 允许我在我的Angular 应用程序中安装生成的项目,它似乎工作正常,如下所示,取自项目的node_modules 目录;
然后我将该模块导入到我的 Angular 应用程序中的模块中;
import { SampleModule } from 'my-test-library';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
NgbModule,
MomentModule,
SampleModule // <-- Imported Here
],
declarations: [],
providers: []
})
export class TasksModule { }
此模块导入导致错误Uncaught Error: Unexpected value 'SampleModule' imported by the module 'TasksModule',我无法弄清楚原因。
在将我导入的库与另一个第三方库(例如ng-bootstrap)进行比较时,我注意到两件事
- 由于我使用
npm link导入库以允许我在开发期间进行测试,因此已导入整个文件夹结构(而不仅仅是dist目录。我认为这意味着正在导入非dist 代码通过TasksModule。如果我尝试导入my-test-library/dist,我会收到模块找不到错误。 - 与
ng-bootstrap不同,我的库在输出中似乎缺少*.metadata.json文件。
我的库的tsconfig.json 文件如下(与生成器安装相同)
{
"compilerOptions": {
"noImplicitAny": true,
"module": "commonjs",
"target": "ES5",
"emitDecoratorMetadata": true, <-- Checked to ensure this was true
"experimentalDecorators": true,
"sourceMap": true,
"declaration": true,
"outDir": "./dist"
},
"files": [
"typings/index.d.ts",
"index.ts"
],
"exclude": [
"node_modules",
"dist"
]
}
基于这些项目或其他内容,我缺少什么来使其正常工作?谢谢!
编辑:sample.*.d.ts 文件(安装后默认)
// sample.component.d.ts
export declare class SampleComponent {
constructor();
}
// sample.directive.d.ts
import { ElementRef } from '@angular/core';
export declare class SampleDirective {
private el;
constructor(el: ElementRef);
}
编辑 2
所以我尝试将dist 目录(my-test-library/dist)符号链接到node_modules 的根目录并从那里导入模块,它工作正常。
- 有没有办法,使用
npm link,只导入dist 目录(在根目录)? - 我也不明白为什么将我的原始导入更新为
import { SampleModule } from 'my-test-library/dist';不起作用?
【问题讨论】:
-
您是否将 system.config 文件设置为将
'my-test-library'映射到某个对象? -
对不起,不确定你指的是哪个文件@echonax,我的项目没有那个文件(它只有上面截图中显示的根级文件)
-
那么,可能是您遇到了这个错误:github.com/angular/angular/issues/11639,这里讨论的是:github.com/angular/angular/issues/11438?如果是这样,您可能会在 9 月 16 日从“Avien”找到可能的答案。在第二个链接中。我只是在这里推测,因为有太多的可能性。希望你能解决这个问题!
-
添加您的 sample.component.d.ts ,sample.directive.d.ts 代码
-
代码添加@Aravind
标签: javascript angular npm