简体   繁体   English

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

[英]Unable to import my locally developed Angular module into Angular app

I'm looking at using this Yeoman generator as a starter for a mini project containing a few reusable form components I can publish. 我正在考虑使用这个Yeoman生成器作为一个迷你项目的入门者,该项目包含我可以发布的一些可重用的表单组件。 The generator builds a module and an example component, directive, pipe and service configured for use ( you can see the template files for the generator here ). 生成器构建模块以及配置使用的示例组件,指令,管道和服务( 您可以在此处查看生成器的模板文件 )。

I then used npm link to allow me to install the generated project in my Angular app, which appears to work fine, as demonstrated below taken from the project's node_modules directory; 然后我使用npm link允许我在我的Angular应用程序中安装生成的项目,这似乎工作正常,如下面从项目的node_modules目录中所示;

代码导入角度应用程序(<code> node_modules </ code>)

I have then imported the module into my module within my Angular app; 然后我在Angular应用程序中将模块导入到我的模块中;

import { SampleModule } from 'my-test-library';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    NgbModule,
    MomentModule,
    SampleModule // <-- Imported Here
  ],
  declarations: [],
  providers: []
})
export class TasksModule { }

This module import causes the error Uncaught Error: Unexpected value 'SampleModule' imported by the module 'TasksModule' and I cannot figure out why. 此模块导入导致错误Uncaught Error: Unexpected value 'SampleModule' imported by the module 'TasksModule' ,我无法弄清楚原因。

There are two things I notice when comparing the library I have imported with another third party library (eg ng-bootstrap ) 将我导入的库与另一个第三方库(例如ng-bootstrap )进行比较时,我注意到两件事

  1. As I have used npm link to import the library to allow me to test during development the entire folder structure has been imported (rather than just the dist directory. I assume this means the non-dist code is being imported by the TasksModule . If I attempt to import my-test-library/dist I get a module cannot be found error. 正如我已经使用npm link导入库,让我在开发期间测试的整个文件夹结构已导入(而不仅仅是dist目录。我认为,这意味着非测距码是由进口TasksModule 。如果我尝试导入my-test-library/dist我得到一个模块无法找到错误。
  2. Unlike ng-bootstrap my library appears to be missing *.metadata.json files in the output. ng-bootstrap不同,我的库似乎缺少输出中的*.metadata.json文件。

My library's tsconfig.json file is as follows (unchanged from generator install) 我的库的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"
  ]
}

Based on these items, or something else, what am I missing to get this working? 根据这些项目或其他内容,我错过了什么让这个工作? Thanks! 谢谢!

Edit: sample.*.d.ts files (as default after installation) 编辑: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);
}

Edit 2 So I tried symlinking the dist directory ( my-test-library/dist ) to the root of node_modules and importing the module from there and it worked fine. 编辑2所以我尝试将dist目录( my-test-library/dist )符号链接到node_modules的根目录并从那里导入模块,它运行正常。

  1. Is there a way to, using npm link , only import the dist directory (at the root)? 有没有办法,使用npm link ,只导入dist目录(在根目录)?
  2. I also don't understand why updating my original import to import { SampleModule } from 'my-test-library/dist'; 我也不明白为什么要更新我的原始导入以import { SampleModule } from 'my-test-library/dist'; does not work? 不起作用?

Did you try to run watch on your code (something like "npm run watch")? 您是否尝试在代码上运行监视(类似“npm run watch”)? That would give a better error message. 这将提供更好的错误消息。

Assuming this is your index.ts file or similar to this ( https://github.com/jvandemo/generator-angular2-library/blob/master/generators/app/templates/index.ts ) What I am guessing is that either your module is not being exported properly or there are some circular dependencies among your exports. 假设这是你的index.ts文件或类似的文件( https://github.com/jvandemo/generator-angular2-library/blob/master/generators/app/templates/index.ts )我猜的是要么您的模块未正确导出或导出之间存在某些循环依赖关系。 First, try changing the following four lines in your index.ts from: 首先,尝试更改index.ts中的以下四行:

export * from './src/sample.component';
export * from './src/sample.directive';
export * from './src/sample.pipe';
export * from './src/sample.service';

to

export {SampleComponent} from "./src/sample.component";
export {SampleDirective} from "./src/sample.directive";
export {SamplePipe} from "./src/sample.pipe";
export {SampleService} from "./src/sample.service";

If that doesnt work, then try changing the order of your exports. 如果这不起作用,那么尝试更改导出的顺序。 If still no luck, then please trying sharing the whole folder somewhere. 如果仍然没有运气,那么请尝试在某处共享整个文件夹。 Thanks, 谢谢,

In your file listing I do not see a sample.module.ts , only a component , directive , pipe and service . 在您的文件列表中,我没有看到sample.module.ts ,只看到componentdirectivepipeservice If you do not have a file with the @ngModule decorator which imports/provides these, then you are not truly importing a SampleModule . 如果你没有带有导入/提供这些文件的@ngModule装饰器的文件,那么你并没有真正导入SampleModule

the file should look something like this: 该文件应如下所示:

sample.modeule.ts sample.modeule.ts

import { NgModule } from '@angular/core';
import { SampleDirective } from './sample.directive';
import { SampleComponent } from '../sample.component';
import { SamplePipe } from './sample.pipe';
import { SampleService } from './sample.service';

@NgModule({
  imports: [ ],
  declarations: [
    SampleDirective,
    SampleComponent,
    SamplePipe
  ],
  providers:[SampleService],
  exports: [
    SampleDirective,
    SampleComponent,
    SamplePipe
  ]
})
export class DayModule { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM