Angular 2 模块 'AppModule' 声明的意外值 'MyDirective'

Angular 2 Unexpected value 'MyDirective' declared by the module 'AppModule'

我正在尝试使用一个指令创建 npm 库并在 npm link 的帮助下在本地测试它。

但问题是当我在声明数组中包含我的指令时,我收到了这个错误:

Unexpected value 'MyDirective' declared by the module 'AppModule'

我的图书馆结构:

package.json

 {
   "name": "my-directive",
   "main": "./dist/index.js",
   "typings":  "dist/index.d.ts"
 }

我的源文件夹:

index.ts

export * from "./myDirective";

myDirective.ts

import {Directive} from '@angular/core';

@Directive({
  selector: "my-directive"
})
export class MyDirective {
  constructor() {
    console.log('directive works!');
  }
}

现在在我的 Angular 2 应用程序中,我正在 linking 这个 npm 包:

import { MyDirective } from "my-directive";

// This line indeed log the constructor function...
console.log(MyDirective);
 // function MyDirective() {
    // console.log('directive works!');
 // }

@NgModule({
  declarations: [
    AppComponent,
    MyDirective
  ]
})

我错过了什么?

这里有关于类似问题的讨论和 PR: https://github.com/angular/angular-cli/pull/2291

看看这是否有帮助。

我遇到了这个问题,我不知道具体原因,但是在 package.json 文件中,删除 Angular 2 依赖项devDependencies 的 dependencies 字段解决了这个问题,我的 npm 模块工作了。

你应该试一试。