如何在Angular中导入模块?

时间:2017-06-20 09:30:35

标签: javascript angular npm

我有一个角度应用程序,我还创建了一个模块作为npm包。这是结构:

--otherModule
  --other-module.module.ts
  --index.ts
  --package.json

index.ts:

export { OtherModule } from './other-module.module';

其他-module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';;

@NgModule({
  declarations: [ OtherComponent ],
  imports: [
    CommonModule
  ],
})
export class OtherModule {
} 

运行npm链接后,我试图在我的AppModule中使用这个模块,如下所示:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { OtherModule } from 'other-module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    OtherModule
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

但后来我收到以下错误:

  

模块'AppModule'导入的意外值'OtherModule'。   请添加@NgModule注释。

看起来装饰工作不起作用。有任何想法吗?

4 个答案:

答案 0 :(得分:1)

import { OtherModule } from 'other-module.module';可行,您错过了为目录结构提供的模块扩展。

答案 1 :(得分:1)

我遇到了同样的问题,在从父应用程序中删除了otherModule和npm-installed的node_modules后解决了这个问题。

但是,必须不断安装和卸载库的node_modules(需要安装才能构建,测试和IDE以了解依赖项中的类型),这会使开发变得非常麻烦。

答案 2 :(得分:0)

我遇到了这样的问题,我通过删除链接文件夹中的node_modules文件夹来修复它。

对于您来说,修复程序应该从--otherModule文件夹中删除node_modules。这样,npm就不会在里面搜索。

我不知道你遇到的问题是否相同,但值得一试。

答案 3 :(得分:0)

您的代码看起来还不错,我已经在自己的机器上对其进行了仿真并对其进行了工作。唯一的不同是,您应该将文件从other-module.module.ts更改为other.module.ts,或者将模块名称从OtherModule更改为OtherModuleModule,然后更新这些引用。

我建议您使用ng g c other到组件和ng g m other到组件的命令通过angular cli创建组件和模块,以保持项目的标准。