【问题标题】:Redirect Empty path to a component将空路径重定向到组件
【发布时间】:2019-07-15 07:06:36
【问题描述】:

因为,我无法将空路径重定向到组件。我在这里遇到了很多问题,但没有一个解决方案能解决它。

当用户第一次点击 URL 时。我希望他重定向到一个组件。

我使用的方法:

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

但它没有将它重定向到那个特定的组件。 Insted 将其重定向到另一个组件。

详细路线配置如下:

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

任何帮助将不胜感激。

【问题讨论】:

  • 应该可以!能否提供用于演示的 stackblitz 代码?
  • 您试图将用户重定向到哪个组件?
  • @SiddAjmera 到 HomeComponent
  • 但是您的HomeComponentAuthGuard 保护。您能否检查一下控件是否会发送到 AuthGuard?如果是,您将用户重定向到哪里?
  • 请分享您对AuthGuard 的实现。您在那里编写的逻辑可能存在问题。

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


【解决方案1】:

也许问题出在 AuthGuard 上。重定向必须有效。 或者,您可以在任何 Main(根)组件导航方法中写入 ngOnInit,让您转到 /home 组件。

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 2017-12-31
    • 1970-01-01
    • 2019-02-22
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多