- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
redirectTo
属性在我的 Angular 2 应用程序中不起作用。我的 app.routing.ts
中有以下路线:
const routes: Routes = [
{ path: '', redirectTo: '/page/1', pathMatch: 'full' },
{ path: 'page', loadChildren: 'app/modules/page/page.module#PageModule' }
]
export const routing = RouterModule.forRoot(routes);
然后,在我的 page.routing.ts
中,我有以下内容:
const pageRoutes: Routes = [
{ path: ':id', component: PageComponent, canActivate: [LoginGuard] }
];
export const pageRouting = RouterModule.forChild(pageRoutes);
每次我访问主页时,它都会显示 LoginComponent
一秒钟,然后消失。但是,它应该重定向到 PageComponent
。
为什么没有发生?如果用户已经登录,为什么要加载 LoginComponent
(即使只是一秒钟)?
这是我的 LoginGuard
:
@Injectable()
export class LoginGuard implements CanActivate {
constructor(private af: AngularFire, private router: Router) {}
canActivate(): Observable<boolean> {
return this.af.auth.map(auth => {
if (auth === null) {
this.router.navigate(['/login']);
return false;
} else {
return true;
}
}).first();
}
}
编辑: 我暂时更改了 LoginComponent
以在用户登录时重定向到 PageComponent
。不过我仍然想知道,为什么 redirectTo
不起作用。
最佳答案
我不知道为什么会这样,但我相信如果您在加载 PageModule 之前检查 LoginGuard,它就会起作用。
应用程序路由.ts
const routes: Routes = [
{ path: '', redirectTo: '/page/1', pathMatch: 'full' },
{
path: 'page',
// Call the guard before the module is loaded
canLoad: [ LoginGuard ]
loadChildren: 'app/modules/page/page.module#PageModule'
}
]
export const routing = RouterModule.forRoot(routes);
登录 guard
@Injectable()
export class LoginGuard implements CanActivate, CanLoad {
constructor(private af: AngularFire, private router: Router) {}
// Add this method to validade the canLoad
canLoad(route: Route): Observable<boolean> {
return this.canActivate();
}
canActivate(): Observable<boolean> {
return this.af.auth.map(auth => {
if (auth === null) {
this.router.navigate(['/login']);
return false;
} else {
return true;
}
}).first();
}
}
关于javascript - 使用 canActivate 防护时 redirectTo 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334906/
我的印象是 header guards 解决了重新定义的问题。我收到链接器错误,指出 .obj 文件中存在重新定义。这是我包含的 header ,问题在于所有全局声明的重新定义。 #ifndef DI
我正在 Microsoft Azure 中运行企业级应用程序。我想知道 Microsoft Azure 中 DDOS 投影的建议是什么。该文档明确指出该平台受到 DDOS 保护,但没有提供更多详细信息
我正在用 laravel sanctum 进行测试,但这里有一些问题.. 我正在创建管理员守卫。 当我将中间件更改为 auth:sanctum_admin .. 它应该只能由管理员访问,但在这里我可以
我有一个带有延迟加载模块的 Angular 4.3.6 应用程序。这是部分根路由器: const routes: Routes = [ { path: '', redirectTo: 'fleet
我有 Vaadin UI 的后端。我想保护后端数据库免受XSS攻击。对于 Vaadin UI 的 XSS 防护,您有何建议? 最佳答案 Vaadin 内置了 XSS 保护。所有组件都正确转义/编码 h
我正在尝试提高我的应用程序的安全性。每当我从用户(无论是通过 POST 还是 GET)接收到应该是整数的数据时,我都会适本地对其进行验证。但通常数据是 VARCHAR,有时可以包含 HTML。 在这种
我有一个具有此功能的服务,它会在 token 有效或无效时返回 true 或 false loggedIn() { return this.http.get('http://localhost:300
我们的应用程序中有几条路线,用户不应通过直接在浏览器的地址栏中输入网址来导航这些路线。 相反,当我们使用 router.navigate() 通过我们的应用程序逻辑以编程方式导航时,我们只想让它们可访
我通常在 .NET 中编写我的 SQL sql.Append("SELECT id, code, email FROM mytable WHERE variable = @variable "); 然
我需要同时将多行(1000 行)插入到 SQL Server 数据库中。我认为最好的方法是使用 SqlBulkCopy 但我不确定如何参数化插入查询以防止 SQL 注入(inject)。 你能帮我一下
Laravel 5.7 PHP 7.2.10 目前我可以使用 Web 和 api 防护中的任何一个,是否有任何方法可以同时允许两者,以便 Web 应用程序和 api 能够协同工作。 类似的东西 ret
Guard 总是因为这个问题而困扰我: .../gems/bundler-1.6.2/lib/bundler/runtime.rb:34:in `block in setup': You have a
我正在尝试通过 gmail api 从 Google 获取邮件 在收到 token 后对 Google_Client 进行身份验证时出现此错误 fatal error :未捕获异常“Google_Au
我是一名优秀的程序员,十分优秀!