代码之家  ›  专栏  ›  技术社区  ›  Luke Xu

确认它是本模块的一部分

  •  0
  • Luke Xu  · 技术社区  · 7 年前

    我觉得我不太明白,组件的声明是如何在角度上工作的。我有两个模块 App Navigation 在导航模块中,我有几个组件, navigation-underline navigation-bar .当我尝试使用 导航下划线 导航栏 我得到的组件 Verify that it is part of this module. 错误

    这是下面的代码

    @NgModule({
      imports: [
        CommonModule
      ],
      declarations: [NavigationBarComponent, NavigationUnderlineComponent]
    })
    export class NavigationModule { }
    
    
    ## navigation-bar.html
    <a style="font-size: 6em;" routerLink="/">Blah Blah</a>
    <nav>
      <a routerLink="/">ABOUT</a>
      <a routerLink="/">RESUME</a>
    </nav>
    <app-navigation-underline></app-navigation-underline>
    

    我试图在应用程序模块中声明它,但它不起作用,我也不需要这样做,因为我只使用 <app-navigation-underline> 在导航模块中。不知道我为什么会出错。

    编辑 :好吧,我好像已经通过导出 NavigationUnderlineComponent 在导航模块中,并将其导入到 AppModule 但我不太明白为什么我需要它。

    编辑2 :实际上我甚至不需要导出它,我只需要在AppModule中声明它。为什么我需要在应用程序模块中声明此组件?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Luke Xu    7 年前

    好吧,我明白了!

    我真正想做的是只在导航模块上下文中使用导航下划线。我是通过以下方式实现的…

    @NgModule({
      imports: [
        CommonModule
      ],
      declarations: [NavigationBarComponent, NavigationUnderlineComponent],
      exports: [NavigationBarComponent]
    })
    export class NavigationModule { }
    

    请注意,未导出NavigationUnderlineComponent。然后在AppModule中

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

    这允许我将导航下划线限制为仅在导航模块中使用