【问题标题】:Gives error to create dynamic tabs in ionic4在 ionic4 中创建动态选项卡时出错
【发布时间】:2019-04-18 15:26:07
【问题描述】:

我正在制作一个 ionic 演示项目,并希望创建一个动态选项卡以及动态添加的选项卡路由。以下是我的源代码,在此代码中动态创建了选项卡,但选项卡的路由不起作用。

tabs.page.html

<ion-tabs  appSwipetab (ionTabsDidChange)="ionTabsDidChange($event)" (tabChange)="onTabChange($event)" #myTabs>

  <ion-tab-bar slot="bottom" color="tertiary">
    <ion-tab-button routerDirection='root' *ngFor="let tab of tabs" [tab]="tab.root">
        <ion-icon [name]="tab.name"></ion-icon>
        <ion-label>{{tab.title}}</ion-label>
    </ion-tab-button>

  </ion-tab-bar>

</ion-tabs>

在 tabs.page.ts 中我设置了静态选项卡和 root.for root 的工作我必须在 tabs.router.module.ts 中添加 root。我希望这将动态工作。

tabs.page.ts

import { Component, ViewChild } from '@angular/core';
import { IonTabs } from '@ionic/angular';

@Component({
  selector: 'app-tabs',
  templateUrl: 'tabs.page.html',
  styleUrls: ['tabs.page.scss']
})

export class TabsPage {

  public tabs;

  constructor() {
   this.tabs = [
    { title: "Schedule", root: 'tab1', name: "calendar" },
    { title: "Speakers", root: 'tab2', name: "contacts" },
    { title: "Map", root: 'tab3', name: "map" },
    { title: "About", root: 'about', name: "person" },

  ];
  }


}

tabs.router.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'tab1',
        children: [
          {
            path: '',
            loadChildren: '../tab1/tab1.module#Tab1PageModule'
          }
        ]
      },
      {
        path: 'tab2',
        children: [
          {
            path: '',
            loadChildren: '../tab2/tab2.module#Tab2PageModule'
          }
        ]
      },
      {
        path: 'tab3',
        children: [
          {
            path: '',
            loadChildren: '../tab3/tab3.module#Tab3PageModule'
          }
        ]
      },
      {
        path: 'about',
        children: [
          {
            path: '',
            loadChildren: '../about/about.module#AboutPageModule'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/tabs/tab1',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/tab1',
    pathMatch: 'full'
  }
];

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

【问题讨论】:

    标签: javascript angular ionic-framework ionic4


    【解决方案1】:

    请按照下面的代码module.ts

      import { NgModule } from '@angular/core';
    import { RouterModule } from '@angular/router';
    import { TabComponent } from './tab.component';
    import { TabRoutes } from './tab.routing';
    
    
    @NgModule({
      imports: [
    
        RouterModule.forChild(TabRoutes)
    
      ],
      declarations: [ TabComponent ]
    })
    
    export class TabModule {}
    

    Tab.routing.module.ts

    从 '@angular/router' 导入 { Routes };

    import { TabComponent } from './tab.component';
    
    export const TabRoutes: Routes = [{
        path: '', 
        redirectTo: 'tab', 
        pathMatch: 'full' 
    },
    {
        path: 'tab',
        component:TabComponent
      }];
    

    【讨论】:

    • 请给我它的完整路径 import { TabRoutingModule }
    • import { TabRoutingModule } './';//添加你的文件路径
    • 它给出了类似的错误..>ERROR 错误:未捕获(在承诺中):错误:无法匹配任何路由。 URL 段:'tabs/%20Tab1Page' 错误:无法匹配任何路由。 URL 段:'tabs/%20Tab1Page' 在
    • 请分享您的 app-routing.module.ts 代码 ...@chrizel
    • 请从头开始检查您在哪里犯了错误。请点击此链接:blog.ionicframework.com/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多