admin管理员组

文章数量:1022964

My goal is : When hit the link 'auth/login/:tokenKey' a method will fire and then redirect A or B ponent. For this link 'auth/login/:tokenKey', no need a ponent. It should be only a method in ts file.

How to do that ?

GetTokenKeyGuard.ts

 canActivate(route: ActivatedRouteSnapshot) {

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

    return true;
  }

I dont need to use a ponent for 'auth/login/:tokenKey' path. In that path, a process will run and then will be redirected to index page.

But when i use 'redirectTo' directive, Guard doesnt work.

When i use with ponent, Guard works.

How to use guard without ponent ?

app-routing.module.ts

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

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

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

My goal is : When hit the link 'auth/login/:tokenKey' a method will fire and then redirect A or B ponent. For this link 'auth/login/:tokenKey', no need a ponent. It should be only a method in ts file.

How to do that ?

GetTokenKeyGuard.ts

 canActivate(route: ActivatedRouteSnapshot) {

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

    return true;
  }

I dont need to use a ponent for 'auth/login/:tokenKey' path. In that path, a process will run and then will be redirected to index page.

But when i use 'redirectTo' directive, Guard doesnt work.

When i use with ponent, Guard works.

How to use guard without ponent ?

app-routing.module.ts

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

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

  { path: 'auth/login/:tokenKey',
    canActivate: [GetTokenKeyGuard],
    ponent: LoginComponent }, //............. Guard works.
];
Share Improve this question edited Feb 9, 2019 at 0:43 canmustu asked Feb 9, 2019 at 0:13 canmustucanmustu 2,6694 gold badges25 silver badges40 bronze badges 2
  • Where do you want to redirect to if the login fails? – Cory Kleiser Commented Feb 9, 2019 at 0:26
  • Actually, i want to run a method when hit 'auth/login/:tokenKey' and then i will redirect to A ponent or B ponent in that method.... Not need a ponent for this link 'auth/login/:tokenKey'.... Post updated. – canmustu Commented Feb 9, 2019 at 0:41
Add a ment  | 

1 Answer 1

Reset to default 8

You can use below path

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

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


My goal is : When hit the link 'auth/login/:tokenKey' a method will fire and then redirect A or B ponent. For this link 'auth/login/:tokenKey', no need a ponent. It should be only a method in ts file.

How to do that ?

GetTokenKeyGuard.ts

 canActivate(route: ActivatedRouteSnapshot) {

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

    return true;
  }

I dont need to use a ponent for 'auth/login/:tokenKey' path. In that path, a process will run and then will be redirected to index page.

But when i use 'redirectTo' directive, Guard doesnt work.

When i use with ponent, Guard works.

How to use guard without ponent ?

app-routing.module.ts

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

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

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

My goal is : When hit the link 'auth/login/:tokenKey' a method will fire and then redirect A or B ponent. For this link 'auth/login/:tokenKey', no need a ponent. It should be only a method in ts file.

How to do that ?

GetTokenKeyGuard.ts

 canActivate(route: ActivatedRouteSnapshot) {

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

    return true;
  }

I dont need to use a ponent for 'auth/login/:tokenKey' path. In that path, a process will run and then will be redirected to index page.

But when i use 'redirectTo' directive, Guard doesnt work.

When i use with ponent, Guard works.

How to use guard without ponent ?

app-routing.module.ts

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

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

  { path: 'auth/login/:tokenKey',
    canActivate: [GetTokenKeyGuard],
    ponent: LoginComponent }, //............. Guard works.
];
Share Improve this question edited Feb 9, 2019 at 0:43 canmustu asked Feb 9, 2019 at 0:13 canmustucanmustu 2,6694 gold badges25 silver badges40 bronze badges 2
  • Where do you want to redirect to if the login fails? – Cory Kleiser Commented Feb 9, 2019 at 0:26
  • Actually, i want to run a method when hit 'auth/login/:tokenKey' and then i will redirect to A ponent or B ponent in that method.... Not need a ponent for this link 'auth/login/:tokenKey'.... Post updated. – canmustu Commented Feb 9, 2019 at 0:41
Add a ment  | 

1 Answer 1

Reset to default 8

You can use below path

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

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


本文标签: javascriptHow to Use Guard Without Component in AngularStack Overflow