2017-06-20 72 views
0

我有一个angular-cli应用程序,我也创建了一个模块作为npm包。这是结构:Angular import 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链接后我试图用这个模块在我的角度cli应用是这样的:

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 { 
} 

但后来我发现了以下错误:

Unexpected value 'OtherModule' imported by the module 'AppModule'. Please add a @NgModule annotation.

好像装饰不工作或东西。有任何想法吗?

+1

你能否提供other-module.module.ts? – JEY

+0

是的,我更新了答案 – ng2user

回答

0

import { OtherModule } from 'other-module.module';会工作,你错过了你提供的目录结构模块扩展。

+0

这不是问题 – ng2user

0

我正面临着这样的问题,我通过删除链接文件夹中的node_modules文件夹窗体来修复它。

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

我不知道你是否面临同样的问题,但值得一试。

+0

但是,您在IDE中出现错误是因为他不知道这些库,不是吗? – ng2user

+0

您应该1 - 从您链接的应用程序中删除node_modules,2 - npm install以安装链接的应用程序及其依赖项。这个whay,链接的应用程序将只有它使用的pkgs,并且角pkgs将被放置在根节点模块中。通过这种方式,您将获得所有引用,但避免在两个node_modules文件夹(应用程序和链接的应用程序)中存在角度pkgs。 – vinagreti

1

我遇到了同样的问题,在删除了其他模块的node_modules和从父应用程序执行的npm-installing后解决了。

但是,必须不断安装和卸载库的node_modules(安装需要用于构建,测试,并且IDE需要知道依赖项中的类型),这会使开发非常繁琐。