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
1 Answer
Reset to default 8You 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
1 Answer
Reset to default 8You 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
版权声明:本文标题:javascript - How to Use Guard Without Component in Angular - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745499280a2153314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论