以角度2进行路由

时间:2016-12-21 09:46:32

标签: javascript html angular single-page-application angular2-routing

我对angular2中的路由有疑问。 我有一个登录屏幕。登录后,仪表板和其他页面的页眉和页脚将不会登录。

const routes: Routes = [
{
    path:'',
    redirectTo: '/login',
    pathMatch: 'full'
},
{
    path:'login',
    loadChildren: './auth/auth.module#AuthModule',
},
{
    path: 'dash',
    loadChildren: './dash/dash.module#DashModule',
    canActivate:[AuthGuard],
    data: {
        preload: true
    }
},
{
    path: 'project',
    loadChildren: './project/project.module#projectModule',
    canActivate: [AuthGuard],
    data: {
        preload: true
    }
}
];

所以它加载到app.component.html中的router-outlet。 目前我必须在所有模块html中使用标头组件,例如dash.component.html

<ks-header></ks-header>
<router-outlet></router-outlet>

这个路由器插座是一个儿童插座,其他破折号相关的负载。
与其他模块一样。 有没有其他有效的方法来显示常见的标题/侧边栏? 我在app.component.html中尝试了它,如

<ks-header [userInfo] ="userInfo" [hidden]="isLogin"></ks-header>
<ks-sidebar [hidden]="isLogin"></ks-sidebar> 

islogin将确定是否已登录。但我认为这不是一个好主意。

1 个答案:

答案 0 :(得分:2)

您应该使用嵌套路线。

一个用于基础,作为模板页面和登录页面之间的路由。

第二个和嵌套的必须在模板化页面之间进行路由,如

HOME, ABOUT, CONTACT ...

您可以从here了解有关嵌套路线的更多信息。这么简单的解释。

another question与你的相似。 Namek的回答似乎很有用。