Angular 4 Routing - 所有路由都被重定向到Default Route

时间:2017-09-06 07:04:50

标签: angular angular4-router teradata-covalent

我为我的angular 4应用程序设置了一个简单的路由,其中​​包含使用Tetradata Covalent and Angular Material创建的sidemenu我使用此Angular 2/4 Authentication tutorial来处理登录,但是所有子路由器链接都重定向到默认页面(仪表板)。任何帮助或建议都将受到高度赞赏。

app.routing.ts

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'}
    ]
  }
];

page.component.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.ts

@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 个答案:

答案 0 :(得分:6)

在您定义路线的app.routing.ts中,在定义了所有路线后,将通配符行移至末尾。

app.routing.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
];

由于它是一个外卡路线,因为路线顺序很重要,因此在点击任何其他路线之前,angular会触及此路线。

答案 1 :(得分:0)

您必须更改类似的代码:

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' }
];