簡體   English   中英

如何在Angular中導入模塊?

[英]How to import module in Angular?

我有一個角度應用程序,我還創建了一個模塊作為npm包。 結構如下:

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

index.ts:

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

其他模塊

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批注。

裝飾器似乎無法正常工作。 有任何想法嗎?

import { OtherModule } from 'other-module.module'; 可以,您錯過了為目錄結構提供的模塊擴展。

我遇到了同樣的問題,從父應用程序刪除了otherModule的node_modules和npm-installing后,它得到了解決。

但是,必須不斷安裝和卸載該庫的node_modules(用於構建,測試和讓IDE知道依賴項中的類型需要安裝),這使開發非常麻煩。

我遇到了這樣的問題,並通過刪除鏈接文件夾中的node_modules文件夾表單解決了該問題。

對於您來說,此修復程序應該是從--otherModule文件夾中刪除node_modules。 這樣,npm不會在其中進行搜索。

我不知道您是否面臨同樣的問題,但是值得一試。

您的代碼看起來還不錯,我已經在我的機器上對其進行了仿真並對其進行了工作。 唯一的不同是,您應該將文件從other-module.module.ts更改為other.module.ts或者將模塊名稱從OtherModuleOtherModuleModule ,然后更新這些引用。

我建議您使用ng gc other to component和ng gm other to module來通過angular cli創建組件和模塊,以保持項目的標准。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM