首先调用模板登录(login.component
)。登录加载后app.component
.
我的问题是,这可能吗?我该怎么做?
Pergunta 编辑:
我已经在使用 Can Activate。对不起,我还在学英语。我想要以下...
bootstrap 首先调用 app.componet。
@Component({
selector: 'my-app',
template: `
<ul class="sidebar-menu">
<li class="header">Painel</li>
<li class="treeview">
<a href="#"><i class="fa fa-link"></i> <span>Loja</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li><a routerLink="/dashboard">Dashboard</a></li>
</ul>
</li>
<li><a routerLink="/users"><i class="fa fa-book"></i> <span>User</span></a></li>
</ul>
<div class="content-wrapper">
<router-outlet></router-outlet>
</div>`,
})
export class AppComponent implements OnInit{}
我要打电话login.component
在调用 app.component
之前.只有在调用用户登录后调用 app.component
.
如果login.component
,菜单将被加载是一条路线,因为菜单将被加载到 <router-outlet></router-outlet>
中.
最佳答案
是的;这是可能的——在 advanced Routing & Navigation 中对此进行了很好的描述文档,特别是在 Milestone #4: Route Guards部分。
需要定义一个CanActivate
守卫,然后用守卫保护路由:
auth-guard.service.ts
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
canActivate() {
console.log('AuthGuard#canActivate called');
return true;
}
}
然后使用守卫来保护您网站中需要身份验证的部分:
admin.routing.ts
import { AuthGuard } from '../auth-guard.service';
const adminRoutes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
children: [
{ path: 'crises', component: ManageCrisesComponent },
{ path: 'heroes', component: ManageHeroesComponent },
{ path: '', component: AdminDashboardComponent }
],
}
]
}
];
export const adminRouting: ModuleWithProviders =
RouterModule.forChild(adminRoutes);
关于Angular 2 - 重定向登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39679623/