无法将我本地开发的Angular模块导入Angular应用程序

Jam*_*ley 11 javascript npm angular

我正在考虑使用这个Yeoman生成器作为一个迷你项目的入门者,该项目包含我可以发布的一些可重用的表单组件.生成器构建模块以及配置使用的示例组件,指令,管道和服务(您可以在此处查看生成器的模板文件).

然后npm link,我曾经允许我在我的Angular应用程序中安装生成的项目,看起来工作正常,如下面从项目node_modules目录中所示;

代码导入角度应用程序(<code>Uncaught Error: Unexpected value 'SampleModule' imported by the module 'TasksModule'</code>,我无法弄清楚原因.</p>

<p>将我导入的库与另一个第三方库进行比较时,我注意到两件事(例如<code>ng-bootstrap</code>)</p>

<ol>
<li>因为我习惯<code>npm link</code>导入库以允许我在开发期间测试整个文件夹结构已被导入(而不仅仅是<code>dist</code>目录.我认为这意味着非dist代码正被导入<code>TasksModule</code>.如果我尝试导入<code>my-test-library/dist</code>我得到一个模块无法找到错误.</li>
<li>与<code>ng-bootstrap</code>我的库不同<code>*.metadata.json</code>,输出中似乎缺少文件.</li>
</ol>

<p>我的库的<code>tsconfig.json</code>文件如下(与发生器安装不变)</p>

<pre><code>import { SampleModule } from 'my-test-library';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    NgbModule,
    MomentModule,
    SampleModule // <-- Imported Here
  ],
  declarations: [],
  providers: []
})
export class TasksModule { }
</code></pre><a target=Run Code Online (Sandbox Code Playgroud)

根据这些项目或其他内容,我错过了什么让这个工作?谢谢!

编辑:sample.*.d.ts文件(安装后默认为)

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

编辑2 所以我尝试将dist目录(my-test-library/dist)符号链接到根目录node_modules并从那里导入模块,它工作正常.

  1. 有没有办法,使用npm link,只导入dist目录(在根目录)?
  2. 我也不明白为什么更新我的原始导入import { SampleModule } from 'my-test-library/dist';不起作用?

Usm*_*man 2

您是否尝试在代码上运行 watch(例如“npm run watch”)?这会给出更好的错误消息。

假设这是您的index.ts文件或与此类似的文件(https://github.com/jvandemo/generator-angular2-library/blob/master/generators/app/templates/index.ts)我猜测是您的模块未正确导出,或者导出之间存在一些循环依赖关系。首先,尝试更改 index.ts 中的以下四行:

export * from './src/sample.component';
export * from './src/sample.directive';
export * from './src/sample.pipe';
export * from './src/sample.service';
Run Code Online (Sandbox Code Playgroud)

export {SampleComponent} from "./src/sample.component";
export {SampleDirective} from "./src/sample.directive";
export {SamplePipe} from "./src/sample.pipe";
export {SampleService} from "./src/sample.service";
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请尝试更改导出的顺序。如果仍然没有成功,请尝试在某个地方共享整个文件夹。谢谢,