5

我已经为我的 Angular 4 应用程序设置了一个简单的路由,其中​​包含一个使用Tetradata Covalent 和 Angular Material创建的侧边菜单我正在使用这个Angular 2/4 身份验证教程来处理登录,但是所有子路由器链接都重定向到默认页面(仪表板)。任何帮助或建议将不胜感激。

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

控制台输出 在此处输入图像描述

4

2 回答 2

13

在定义所有路由后,在定义路由的 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
];

由于它是通配符路线,因此由于路线顺序很重要,因此角度将在到达任何其他路线之前先到达该路线。

于 2017-09-06T07:06:38.857 回答
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' }
];
于 2017-09-12T09:33:53.033 回答