【问题标题】:Tab is not working in the expected way -Angular Material选项卡未按预期方式工作 - Angular Material
【发布时间】:2020-08-09 16:41:48
【问题描述】:

我只是想学习角度。我正在使用 Angular 8 和最新材料。我正在分享我的 dashboard.components.html

<div><nav mat-tab-nav-bar>
    <a mat-tab-link [routerLink]="['/home']">Home</a>
    <a mat-tab-link [routerLink]="['/admin']">Admin</a>
    <a mat-tab-link [routerLink]="['/order']">Orders</a>
    </nav></div>
  <div><router-outlet></router-outlet></div>

AppRoutingModule.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { HomeComponent} from './components/home/home.component';
import { AdminComponent} from './components/admin/admin.component';
import { OrdersComponent} from './components/orders/orders.component';


const routes: Routes = [
  {path: 'dashboard', component: DashboardComponent 
  {path: 'home', component: HomeComponent},
  {path: 'admin', component: AdminComponent},
  {path: 'order', component: OrdersComponent},
  {path: '', redirectTo: 'dashboard', pathMatch: 'full'}
];

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

这里的问题是当我点击标签页时,标签页正在加载但标签屏幕消失了。我希望 Nab bar 选项卡应该一直存在。在该选项卡导航栏的正下方,页面应该被加载。这里没有发生。我一直都必须点击后退按钮。谁能告诉我我在这里缺少什么。

【问题讨论】:

    标签: angular angular-material nav mat-tab


    【解决方案1】:

    您需要为“仪表板”路径指定子路由。因此,您的应用将加载 DashboardComponent(带有选项卡内容)和 router-outlet(选定的子项)

       const routes: Routes = [
          {path: 'dashboard', component: DashboardComponent 
            children: [
             {path: 'home', component: HomeComponent},
             {path: 'admin', component: AdminComponent},
             {path: 'order', component: OrdersComponent}
            ]
          },
          {path: '', redirectTo: 'dashboard', pathMatch: 'full'}
       ];
    

    【讨论】:

    • 有什么方法可以将默认活动路径设置为 home ? ,我使用 {path: '', redirectTo: 'home', pathMatch: 'full'} 做到了,并且成功了。但默认选项卡虽然显示页面但未激活。
    • 尝试重定向到仪表板/主页
    • 没有。它不工作。标签本身应该有一些东西来设置默认选项卡被选中。
    • 我会发布另一个问题,如果它在经过几次尝试后仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多