- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
redirectTo
无法重定向到带有 canActivate
的链接当我登录时.
redirectTo
当我注销时工作正常,它重定向到我的 /login
组件和 redirectTo
至 /index
登录后。
但它坚持''
当我登录时。
我已经添加了 console.log
到我的authGuard
, 并且重定向 url "/index"确实在该函数中进入了 ''
开发工具没有崩溃的情况。
但在评论 canActivate
之后, redirectTo
像以前一样工作正常。
顺便说一下,我的 authGuard 是 canActivate
在其他情况下工作正常(不包括 redirecTo
)
这是我的 app-routing.module.ts
/* import... */
const appRoutes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo : '/index', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
/* ngmodel and export ... */
这是我的 index-routing.module.ts
/* import ... */
const indexRoutes: Routes = [
{
path: 'index',
component: IndexComponent,
canActivate: [AuthGuard], //<- if comment this will work
},
];
/* ngmodel and export ... */
这是我的 auth-guard.module.ts
/* import ... */
@Injectable()
export class AuthGuard implements CanActivate {
constructor(
private authService: AuthService,
private router: Router
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;
//redirectTo did get in here
console.log(url); //<-- this show /index
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.authService.login_data && (this.authService.login_data.userprofile || !this.authService.login_data.need_login)) {
return true;
}
this.authService.get_login_data()
.subscribe(
data => {
this.authService.login_data = data;
if (data.userprofile || !data.need_login) {
return true;
}
this.authService.redirectUrl = url;
this.router.navigate(['/login']);
return false;
},
error => { this.authService.errMsg = <any>error;}
);
}
}
顺便说一句,我已经将我的项目更新为angular4.0.1
谢谢。
更新:
我已经将我的守卫返回从 bool 值更改为可观察的
但还是不行。
这是我用过的代码。
checkLogin(url: string): Observable<boolean> {
if (this.authService.login_data && (this.authService.login_data.userprofile || !this.authService.login_data.need_login)) {
console.log('gonna return observable true');
return Observable.of(true);
//return true;
}
this.authService.get_login_data()
.subscribe(
data => {
this.authService.login_data = data;
if (data.userprofile || !data.need_login) {
console.log('in subscribe and gonna return observable true');
return Observable.of(true);
//return true;
}
this.authService.redirectUrl = url;
this.router.navigate(['/login']);
return Observable.of(false);
//return false;
},
error => { this.authService.errMsg = <any>error;}
);
}
在我的控制台中,我得到了 in subscribe and gonna return observable true
更新:
检查后检查此后
AuthGuard doesn't wait for authentication to finish before checking user
问题可能是canActivate
不等待订阅结束。
最佳答案
在 AuthGuard
的 checkLogin()
中:
checkLogin(url: string): boolean {
if (this.authService.login_data && (this.authService.login_data.userprofile || !this.authService.login_data.need_login)) {
return true;
}
this.authService.get_login_data()
.subscribe(
data => {
this.authService.login_data = data;
if (data.userprofile || !data.need_login) {
return true;
}
this.authService.redirectUrl = url;
this.router.navigate(['/login']);
return false;
},
error => { this.authService.errMsg = <any>error;}
);
}
它的返回类型是boolean,它返回true,在某些情况下,它是可以的。但是,后面的部分有一些问题。在您订阅结果并根据该结果返回之前,您没有返回任何内容。
这意味着,此函数将返回未定义,因此 canActivate 检查将不起作用。
[更新] 你想从结果中设置用户数据,你应该在你的 authService.get_login_data()
中使用 do()
来设置它。当您订阅结果时,将调用此“do”函数。在您的 checkLogin()
中使用 .map()
而不是 subscribe()
将登录结果数据映射到 bool 值。
在这种情况下,您应该返回一个 Observable 类型,例如:
checkLogin(url: string): Observable<boolean> {
if (this.authService.login_data && (this.authService.login_data.userprofile || !this.authService.login_data.need_login)) {
return Observable.of(true); // wrap true in Observable
}
return this.authService.get_login_data()
.map(
data => {
if (data.userprofile || !data.need_login) {
return true;
}
this.authService.redirectUrl = url;
this.router.navigate(['/login']);
return false;
},
error => { this.authService.errMsg = <any>error;}
);
关于重定向到带有 canActivate 的链接时,Angular2+ redirectTo 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928963/
在我的 Angular 8 应用程序中,我有 3 个延迟加载模块。每个模块都针对特定用户的角色。我正在为 Auth 和 Role Guard 使用 canActivate 接口(interface)。
在我的 Angular 8 应用程序中,我有 3 个延迟加载模块。每个模块都针对特定用户的角色。我正在为 Auth 和 Role Guard 使用 canActivate 接口(interface)。
我的 $routeprovider 有一个配置,当我将 redirectTo: function() {'/dasboard'} 更改为 redirectTo: function() {'/home'
我第一次开发 Firefox 扩展,多亏了文档,它进展得相当快。但是我有一个问题:如果用户访问某些域,我不想重定向他们。 const {Cc, Ci, Cr, Cu} = require("chrom
给定路由配置 @RouteConfig([ new Route({ path: '/app/index.html', name: 'default', redirectTo: ['/View1
我最近开始了一个基于 Angular 2.0.0 并结合 Angular Router (V3.0.0) 的新项目。因此,我对辅助路线有疑问。 我想将我的应用程序拆分为出现在导航栏、主要内容、页脚等每
一切都在标题中:您可以使用 redirectTo 属性重定向到相对 URL 吗? 我的(简化的)路由如下: {path: 'login', children: []}, {path: '', chil
我正在开发一个 Angular 应用程序,它具有以下URL路径, /(根目录) /类(class) /类(class)/创建 /类(class)/:id /验证/登录 我已为除 auth/login
假设我有以下代码: ... routes: { 'someurl:{param}': { action: 'action', before: 'beforeac
谁能告诉我component、loadChildren、redirectTo之间的区别? export const ROUTES: Routes = [{ path: '', redirectT
我正在使用一个函数来确定在加载网站时应将用户重定向到何处。像这样: { path : '', redirectTo: redirector(), pathMatch: 'full' } r
我正在使用 angular 2 开发我的第一个应用程序,我遇到了一个奇怪的问题。 我的路由配置是这样的: export const routes: RouterConfig = [ { path: '
我们可以覆盖此属性以在 LoginController 中登录后重定向用户: protected $redirectTo = '/home'; 这是文档中的声明: If the redirect pa
我有这样的路线 const pipelinesRoutes: Routes = [ { path: ':type', component: CatalogComponent,
我只是想使用参数重定向到错误页面。它被编码为 /error%3Ftype=404。 这会破坏路由。该路线永远不会激活。通过输入或定位,一切都变得魅力十足。 otherwise( { redir
redirectTo无法重定向到带有 canActivate 的链接当我登录时. redirectTo当我注销时工作正常,它重定向到我的 /login组件和 redirectTo至 /index登录后
我的 RouteConfig 定义了以下路径 @RouteConfig ([ { path:'/main',name:'Home',redirectTo: ['Setup']}, {
redirectTo 属性在我的 Angular 2 应用程序中不起作用。我的 app.routing.ts 中有以下路线: const routes: Routes = [ { path: ''
我有一些路由模块,其主要路径设置为:/canvas const canvasRoutes: Routes = [ { path: "canvas", component: Ca
如果可以将路径:''重定向到路径:':id',我正在徘徊?有什么办法可以实现吗? 谢谢。 { path: 'folder', children: [ { path: '',
我是一名优秀的程序员,十分优秀!