4
关注
2052
浏览

我如何在angular4中设置默认路由?

为什么被折叠? 0 个回复被折叠
kisshc 未验证用户 用户来自于: 广东省
2020-09-18 00:10
您可以使用这样的, // otherwise redirect to home { path: '**', redirectTo: '/' }
chingfeng 未验证用户 用户来自于: 广东省
2020-09-16 21:48

请更改您的应用程序路线顺序并添加pathMatch属性。

const appRoutes: Routes = [ 
    { path: 'login', component: LoginComponent }, 
    { path: 'register', component: RegisterComponent }, 
    { path: '', component: HomeComponent, canActivate: [AuthGuard], pathMatch: 'full' }, 

    // otherwise redirect to home 
    { path: '**', redirectTo: '' } 
]; 
gaomiao 未验证用户 用户来自于: 广东省
2020-09-16 12:04

在您的AuthGardcanActivate方法,在false答案的情况下,您可以简单地将客户端重定向到/register

export class AuthGuard { 

    constructor(private authService : AuthService, private router : Router) { 
    } 

    canActivate(route : ActivatedRouteSnapshot, state : RouterStateSnapshot) { 
    if(this.authService.isLoggedIn()) 
     return true; 
    else // This: 
     this.router.navigate(['DESIRED/PATH']); 
    } 
} 

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 16:04
关注人数
4 人关注

相关问题

如何在codeigniter中使用ajax上传文件
我如何从火力地堡/公司的FireStore通过属性值排序文件
经济型三位显示模糊PID温控器,参数应该怎么设置?
使用Javascript设置高度很慢
9、我如何在养发到家小程序上查找我的所有消费情况?
投票小程序制作,如何在乔拓云平台复制模板创建投票小程序?
如何设置独立服务器?
如何在Odoo 8中使用JavaScript?
如何在安卓手机上制作个win8.1系统的修复盘
按键精灵如何在找到颜色后再次确认并进行触发?
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号