代码之家  ›  专栏  ›  技术社区  ›  Isaac Obella

角度4路由-所有路由重定向到默认路由

  •  5
  • Isaac Obella  · 技术社区  · 7 年前

    我已经为我的angular 4应用程序设置了一个简单的路由,其中包括一个使用创建的侧菜单 Tetradata Covalent and Angular Material 我在用这个 Angular 2/4 Authentication tutorial

    export const APP_ROUTES: Routes = [
      {path: 'login', component: HomeComponent},
      { path: '', redirectTo: 'mainpage', pathMatch: 'full' },
      { path: '**', redirectTo: 'mainpage' },
      {
        path: 'mainpage',
        component: MainPageComponent,
        canActivate:[AuthGuard],
        children: [
          {path: 'order', component: OrderComponent},
          {path: 'dashboard', component: DashboardComponent},
          {path: 'stock', component: StockComponent},
          {path: 'Report/sales', component: SalesComponent},
          {path: '', redirectTo: 'dashboard', pathMatch: 'full'}
          {path: '**', redirectTo: 'dashboard', pathMatch: 'full'}
        ]
      }
    ];
    

    页组成部分html

    <td-layout>
      <td-navigation-drawer flex sidenavTitle="EzyAgric" logo="assets:logo" name="Akorion" email="info.akorion.com">
    <md-nav-list>
          <a md-list-item [routerLink]="['dashboard']">
            <md-icon>dashboard</md-icon>
            Dashboard</a>
          <md-divider></md-divider>
          <h3 md-subheader>Orders</h3>
          <a md-list-item [routerLink]="['stock']">
            <md-icon>archive</md-icon>
            Stock
          </a>
          <a md-list-item [routerLink]="['order']">
            <md-icon>shopping_cart</md-icon>
            Received Orders
          </a>
      </td-navigation-drawer>
      <td-layout-nav [toolbarTitle]="getTitle()" >
        <div class="example-scrolling-content">
          <router-outlet></router-outlet>
        </div>
      </td-layout-nav>
    </td-layout>
    

    AuthGuard。输电系统

    @Injectable()
    export class AuthGuard implements CanActivate {
      constructor(private router: Router) { }
    
      canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        if (localStorage.getItem('currentUser')) {
          // logged in so return true
          return true;
        }
    
        // not logged in so redirect to login page with the return url
        this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
        return false;
      }
    }
    

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  13
  •   Chic    7 年前

    在你的应用程序中。路由。定义路由的ts在定义所有路由后,将通配符线移动到末尾。

    应用程序。路由。输电系统

    export const APP_ROUTES: Routes = [
      {path: 'login', component: HomeComponent},
      { path: '', redirectTo: 'mainpage', pathMatch: 'full' },
      {
        path: 'mainpage',
        component: MainPageComponent,
        canActivate:[AuthGuard],
        children: [
          {path: 'order', component: OrderComponent},
          {path: 'dashboard', component: DashboardComponent},
          {path: 'stock', component: StockComponent},
          {path: 'Report/sales', component: SalesComponent},
          {path: '', redirectTo: 'dashboard', pathMatch: 'full'}
          {path: '**', redirectTo: 'dashboard', pathMatch: 'full'}
        ]
      },
      { path: '**', redirectTo: 'mainpage' } // this needs to be after other routes
    ];
    

    因为这是一条外卡路线,所以在到达任何其他路线之前,角度会先到达该路线,因为路线顺序很重要。

        2
  •  0
  •   Salim Ibrohimi    7 年前

    export const APP_ROUTES: Routes = [
      { path: '', redirectTo: 'mainpage', pathMatch: 'full' },
      { path: 'login', component: HomeComponent },  
      {
        path: 'mainpage',
        component: MainPageComponent,
        canActivate:[AuthGuard],
        children: [
          {path: 'order', component: OrderComponent},
          {path: 'dashboard', component: DashboardComponent},
          {path: 'stock', component: StockComponent},
          {path: 'Report/sales', component: SalesComponent}
        ]
      },
      { path: '**', redirectTo: 'mainpage' }
    ];