angular - 'ion-header' 不是已知元素 ionic 5

标签 angular ionic-framework

以下是我已采取的步骤

  • 我使用 创建了一个 ionic 5 的新项目 ionic 启动模板空白
  • 将 Angular 更新为 Angular 9
  • 使用 创建了一个新的页面模块ng g 主页
  • 使用延迟加载在 AppRoutingModule 中列出。

  • 我收到以下错误
    ERROR in src/app/pages/main/main.page.html:1:1 - error NG8001: 'ion-header' is not a known element:
    1. If 'ion-header' is an Angular component, then verify that it is part of this module.
    2. If 'ion-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    
    1 <ion-header>
      ~~~~~~~~~~~~
    
      src/app/pages/main/main.page.ts:5:16
        5   templateUrl: './main.page.html',
                         ~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component MainPage.
    src/app/pages/main/main.page.html:2:3 - error NG8001: 'ion-toolbar' is not a known element:
    1. If 'ion-toolbar' is an Angular component, then verify that it is part of this module.
    2. If 'ion-toolbar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress
    this message.
    
    2   <ion-toolbar>
        ~~~~~~~~~~~~~
    
      src/app/pages/main/main.page.ts:5:16
        5   templateUrl: './main.page.html',
                         ~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component MainPage.
    src/app/pages/main/main.page.html:3:5 - error NG8001: 'ion-title' is not a known element:
    1. If 'ion-title' is an Angular component, then verify that it is part of this module.
    2. If 'ion-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    
    3     <ion-title>main</ion-title>
          ~~~~~~~~~~~
    
      src/app/pages/main/main.page.ts:5:16
        5   templateUrl: './main.page.html',
                         ~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component MainPage.
    src/app/pages/main/main.page.html:7:1 - error NG8001: 'ion-content' is not a known element:
    1. If 'ion-content' is an Angular component, then verify that it is part of this module.
    2. If 'ion-content' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress
    this message.
    
    7 <ion-content>
      ~~~~~~~~~~~~~
    

    我错过了一些非常明显的东西。

    以下是代码

    app.module.ts
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { RouteReuseStrategy } from '@angular/router';
    
    import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
    import { SplashScreen } from '@ionic-native/splash-screen/ngx';
    import { StatusBar } from '@ionic-native/status-bar/ngx';
    
    import { AppComponent } from './app.component';
    import { AppRoutingModule } from './app-routing.module';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    @NgModule({
      declarations: [AppComponent],
      entryComponents: [],
      imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, BrowserAnimationsModule],
      providers: [
        StatusBar,
        SplashScreen,
        { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule {}
    

    应用程序路由.module.ts
    import { NgModule } from '@angular/core';
    import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
    
    const routes: Routes = [
      {
        path: '',
        loadChildren: () => import('./pages/main/main-routing.module').then(m => m.MainPageRoutingModule)
      },
    ];
    
    @NgModule({
      imports: [
        RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
      ],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    

    主模块.ts
    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { FormsModule } from '@angular/forms';
    
    import { IonicModule } from '@ionic/angular';
    
    import { MainPageRoutingModule } from './main-routing.module';
    
    import { MainPage } from './main.page';
    
    @NgModule({
      imports: [
        CommonModule,
        FormsModule,
        IonicModule,
        MainPageRoutingModule
      ],
      declarations: [MainPage]
    })
    export class MainPageModule {}
    

    主页.ts
    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'sxm-main',
      templateUrl: './main.page.html',
      styleUrls: ['./main.page.scss'],
    })
    export class MainPage implements OnInit {
    
      constructor() { }
    
      ngOnInit() {
      }
    
    }
    

    main.page.html
    <ion-header>
      <ion-toolbar>
        <ion-title>main</ion-title>
      </ion-toolbar>
    </ion-header>
    
    <ion-content>
    
    </ion-content>
    

    最佳答案

    就我而言,我遇到了奇怪的 html 模板错误,包括一个 OP 描述的错误,因为我创建了新组件并忘记将其添加到模块声明中

    app.module.ts

    @NgModule({
    ...
    declarations: [MyComponent]
    }
    

    关于angular - 'ion-header' 不是已知元素 ionic 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338913/

    相关文章:

    javascript - 按 2 个日期进行 Angular 过滤

    angular - Ionic 和 Angular : "::ng-deep" works with CSS, 但没有预处理器 (SASS/SCSS)

    angularjs - 如何使用手机号码和 otp ionic 框架应用程序对用户进行身份验证

    angular - 意外值 ‘HttpClient’

    angularjs - 构建用于服务器端过滤和分页的 Angular 应用程序

    angular - 如何使用 RxJS 在 Angular 6 中发出一系列 http 请求

    angular - 在 ag-grid 中隐藏列名?

    android - 如何为 Appium 中使用的测试数据库建立种子?

    angular - @Output 事件发射器不工作

    css - ionic 图标周围的阴影/轮廓