簡體   English   中英

如何在沒有Angular組件的情況下使用Guard

[英]How to Use Guard Without Component in Angular

我的目標是:當單擊鏈接'auth/login/:tokenKey'將觸發一種方法,然后重定向A或B組件。 對於此鏈接'auth/login/:tokenKey' ,不需要組件。 它應該只是ts文件中的一種方法。

怎么做 ?

GetTokenKeyGuard.ts

 canActivate(route: ActivatedRouteSnapshot) {

    localStorage.setItem('token_key', route.params.tokenKey);

    return true;
  }

我不需要為'auth/login/:tokenKey'路徑使用組件。 在該路徑中,將運行一個進程,然后將其重定向到索引頁。

但是,當我使用“ redirectTo”指令時,Guard無法正常工作。

當我與組件一起使用時,Guard可以工作。

如何使用無組件的防護?

app-routing.module.ts

const routes: Routes = [
  { path: '', component: IndexComponent },
  { path: 'auth/login', component: LoginComponent },

  { path: 'auth/login/:tokenKey',
    canActivate: [GetTokenKeyGuard],
    redirectTo: '' }, //........................ Guard doesnt work.

  { path: 'auth/login/:tokenKey',
    canActivate: [GetTokenKeyGuard],
    component: LoginComponent }, //............. Guard works.
];

您可以使用以下路徑

 const routes: Routes = [
  { path: '', component: IndexComponent },
  { path: 'auth/login', component: LoginComponent },

  { path: 'auth/login/:tokenKey',
    canActivate: [GetTokenKeyGuard],
    children: [] }
];


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM