将空路径重定向到组件

时间:2019-07-15 07:06:36

标签: angular angular-routing angular-router angular-router-guards

作为,我无法将空路径重定向到组件。我在这里经历了很多问题,但是没有一种解决方案可以解决。

用户首次点击URL时。我希望他重定向到组件。

我使用的方法:

{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },

,但不会将其重定向到该特定组件。将其重定向到另一个组件。

详细列出了路由配置:

const routes: Routes = [
  { path: 'login', component: LoginComponent },
  { path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
  {
    path: 'notifications',
    component: NotificationComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'drivers',
    loadChildren: './drivers/drivers.module#DriversModule',
    canActivate: [AuthGuard]
  }, {
    path: 'managers',
    loadChildren: './users/users.module#UsersModule',
    canActivate: [AuthGuard]
  }, {
    path: 'vendors',
    loadChildren: './vendors/vendors.module#VendorsModule',
    canActivate: [AuthGuard]
  }, {
    path: 'settings',
    loadChildren: './settings/settings.module#SettingsModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'orders',
    loadChildren: './orders/orders.module#OrdersModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'customers',
    loadChildren: './customers/customers.module#CustomersModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'payments',
    loadChildren: './payments/payments.module#PaymentsModule',
    // canActivate: [AuthGuard]
  },
  { path: 'orders/:id', component: OrderDetailsComponent },
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: '**', component: LoginComponent }

];

我使用的Angular配置:

  "dependencies": {
    "@angular/animations": "^6.1.0",
    "@angular/cdk": "^6.4.6",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/flex-layout": "^6.0.0-beta.17",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/material": "^6.4.6",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "@types/socket.io-client": "^1.4.32",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "socket.io-client": "^2.1.1",
    "zone.js": "~0.8.26"
  },

AuthGurad:

export class AuthGuard implements CanActivate, CanActivateChild {
  constructor(private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    if (!localStorage.getItem('token')) {
      this.router.navigate(['/login']);
      return false;
    } else {
      return true;
    }
  }

  canActivateChild(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    if (!localStorage.getItem('token')) {
      this.router.navigate(['/login']);
      return false;
    } else {
      return true;
    }
  }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

也许问题出在AuthGuard。重定向必须有效。 另外,当您转到/ home组件时,可以使用任何主(根)组件导航方法写入ngOnInit。