简体   繁体   English

Angular 2+一个路由由两个组件按条件处理

[英]Angular 2+ One route handled by two components by condition

My app has private and public parts. 我的应用程序有私人和公共部分。

If user not logged in then he should see landing page with some 'about text', facebook/twitter links and a link to login/register as a root page of application. 如果用户没有登录,那么他应该看到有一些“关于文本”的登陆页面,facebook / twitter链接以及登录/注册为应用程序根页面的链接。

If he is logged in - he should get a dashboard page filled with user specific data as a root of whole application. 如果他已登录 - 他应该获得一个充满用户特定数据的仪表板页面作为整个应用程序的根目录。

And surely, it would be nice to have both of these pages handled by one root route "/". 当然,通过一个根路由“/”处理这两个页面会很好。

So i wonder, what is the right way to implement it with angular 2+ and native router? 所以我想知道,使用angular 2+和本地路由器实现它的正确方法是什么?

export const ROUTES: Routes = [{
    path: '/',
    component: isAuthenticated() ? DashboardComponent : LandingPageComponent
}]

Consider using a canActivate guard: 考虑使用canActivate防护:

export const ROUTES: Routes = [
    {path: '/', canActivate: [AuthGuard], component: LandingPageComponent},
    {path: '/landing', component: LandingPageComponent}
},

] ]

In the guard you can check for a login and reroute to the Landing page. 在警卫中,您可以检查登录并重新路由到登陆页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM