2017-06-20 86 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需要知道依賴項中的類型),這會使開發非常繁瑣。