Пользовательский компонент Ionic 5 angular не отображается

#angular #ionic-framework

#угловой #ionic-framework

Вопрос:

У меня есть родительская страница, которая содержит несколько дочерних страниц и панель вкладок ion tabs.page.html

 <!-- tabs.page.html -->
<ion-tabs>
  <ion-tab-bar slot="bottom">
    <ion-tab-button tab="home">
      <ion-icon name="home"></ion-icon>
      <ion-label>Home</ion-label>
    </ion-tab-button>
    <ion-tab-button tab="create-post">
      <ion-icon name="add-outline"></ion-icon>
      <ion-label>Create</ion-label>
    </ion-tab-button>
    <ion-tab-button tab="profile">
      <ion-icon name="person"></ion-icon>
      <ion-label>Profile</ion-label>
    </ion-tab-button>
  </ion-tab-bar>
</ion-tabs>
 

Одна из дочерних страниц является домашней, когда я вызываю пользовательский компонент, названный здесь app-test, шаблон (html) не отображается.

 <!-- home.page.html -->
<ion-content>
  <h1>Hello</h1> <!-- Displayed -->
  <app-test></app-test> <!-- doesn't display anything -->
  <h1>Hello 2</h1> <!-- Displayed -->
</ion-content>
 

test.component.ts

 import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss'],
})
export class TestComponent implements OnInit {

  constructor() { }

  ngOnInit() {}

}
 

test.component.html

 <p>hello</p>

 

home.page.html

 <ion-content>
  <h1>Hello</h1> <!---->
  <app-test></app-test>
  <h1>Hello 2</h1>
</ion-content>
 

home.module.ts

 import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { HomePageRoutingModule } from './home-routing.module';

import { HomePage } from './home.page';
import {TabsPagePageModule} from '../tabs-page/tabs-page.module';
import {TestComponent} from '../../components/test/test.component';
import {AppModule} from '../../app.module';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    HomePageRoutingModule,
    TabsPagePageModule,
    AppModule,
  ],
  declarations: [HomePage],
  entryComponents: []
})
export class HomePageModule {}
 

главная страница-routing.module.ts

 import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { HomePage } from './home.page';

const routes: Routes = [
  {
    path: '',
    component: HomePage
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class HomePageRoutingModule {}
 

home.page.ts

 import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}
 

tabs-page.component.html

 <ion-tabs>
  <ion-tab-bar slot="bottom">
    <ion-tab-button tab="home">
      <ion-icon name="home"></ion-icon>
      <ion-label>Home</ion-label>
    </ion-tab-button>
    <ion-tab-button tab="create-post">
      <ion-icon name="add-outline"></ion-icon>
      <ion-label>Create</ion-label>
    </ion-tab-button>
    <ion-tab-button tab="profile">
      <ion-icon name="person"></ion-icon>
      <ion-label>Profile</ion-label>
    </ion-tab-button>
  </ion-tab-bar>
</ion-tabs>
 

tabs-page.component.ts

 import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-tabs-page',
  templateUrl: './tabs-page.component.html',
  styleUrls: ['./tabs-page.component.scss'],
})
export class TabsPage implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}
 

tabs-page.module.ts

 import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { TabsPagePageRoutingModule } from './tabs-page-routing.module';

import { TabsPage } from './tabs-page.component';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    TabsPagePageRoutingModule
  ],
  exports: [
    // TestComponent
  ],
  declarations: [TabsPage]
})
export class TabsPagePageModule {}
 

tabs-page-routing.module.ts

 import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { TabsPage } from './tabs-page.component';
import {HomePage} from '../home/home.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'home',
        children: [
          {
            path: '',
            component: HomePage
          }
        ]
      },
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class TabsPagePageRoutingModule {}
 

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 { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import {TestComponent} from './components/test/test.component';

@NgModule({
  declarations: [AppComponent, TestComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [{provide: RouteReuseStrategy, useClass: IonicRouteStrategy}],
  bootstrap: [AppComponent],
  exports: [
    TestComponent
  ]
})
export class AppModule {}
 

app-routing.module.ts

 import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: 'login',
    loadChildren: () => import('./pages/login/login.module').then( m => m.LoginPageModule)
  },
  {
    path: 'tabs-page',
    loadChildren: () => import('./pages/tabs-page/tabs-page.module').then( m => m.TabsPagePageModule)
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }
 

Обновить:

при использовании http://localhost:8100/home , компонент отображается, но при использовании http://localhost:8100/tabs/home , это не сработает

Почему не отображается HTML-код app-test?

Комментарии:

1. Пожалуйста, также поделитесь с нами TestComponent.ts.

2. @Beller Я поделился всем кодом. Заранее благодарю вас.

3. в app.module.ts remvove export и добавьте TestComponent в entryComponents

4. @ChetanBansal Я пробовал, это не сработало.

5. Поскольку вы используете его в своем HomePageModule, именно там он должен быть объявлен.