代码之家  ›  专栏  ›  技术社区  ›  Anthony O.

在离子4中创建侧菜单作为组件

  •  0
  • Anthony O.  · 技术社区  · 5 年前

    我用 ionic start myApp sidemenu .

    现在我只希望侧菜单是一个组件。

    所以我创造了:

    • /src/app/menu/menu.component.ts :
    @Component({
      selector: 'app-menu',
      templateUrl: './menu.component.html',
    })
    export class MenuComponent implements OnInit {
    
      // [...] code copied from original generated app.component.ts
    
    }
    
    • /src/app/menu/menu.component.html 从原始生成的代码 app.component.html ,
    • /src/app/menu/menu.module.ts :
    @NgModule({
      imports: [
        CommonModule,
        IonicModule,
        RouterModule
      ],
      declarations: [MenuComponent],
      exports: [MenuComponent]
    })
    export class MenuComponentModule {}
    
    • 被改进的 /src/app/app.module.ts :
    @NgModule({
      declarations: [AppComponent],
      entryComponents: [MenuComponent],
      imports: [
        BrowserModule,
        IonicModule.forRoot(),
        AppRoutingModule,
        MenuComponentModule
      ],
      // [...] providers & bootstrap identicall as original
    })
    export class AppModule {}
    
    • 被改进的 /src/app/app.component.html 简单地说这个文件:
    <ion-app>
      <ion-split-pane>
        <ion-menu>
          <app-menu></app-menu>
        </ion-menu>
        <ion-router-outlet main></ion-router-outlet>
      </ion-split-pane>
    </ion-app>
    

    但是没有显示任何内容,控制台中出现此错误我不知道为什么:

    Error: Template parse errors:
    'app-menu' is not a known element:
    1. If 'app-menu' is an Angular component, then verify that it is part of this module.
    2. If 'app-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
      <ion-split-pane>
        <ion-menu>
          [ERROR ->]<app-menu></app-menu>
        </ion-menu>
        <ion-router-outlet main></ion-router-outlet>
    "): ng:///AppModule/AppComponent.html@3:6
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Ehsan Kiani    5 年前

    据我所知,你的代码没有问题,但我有个建议,声明 Menu组件 直接在 应用模块.ts . 也许会有帮助