关于新的 angular2 路由器还有一些问题和现在的信息。
首先,新的@Routes 中没有“名称”,因此应该始终存在相对 URL 路径,这没关系。
但是我怎样才能检查哪条是我激活的路线?在不推荐使用的路由器中,我这样做了:
let instruction = this.router.generate(['Login']);
let isLoginRoute = this.router.isRouteActive(instruction);
另一件事是,这是检查用户是否有权导航到路由的最佳方法。什么是它的最佳实践,直到现在我做了这样的事情:
@CanActivate((next: ComponentInstruction, previous: ComponentInstruction) => {
return isLoggedIn(next, previous);
})
export class DashboardCmp implements OnDestroy {
...
export const isLoggedIn = (next: ComponentInstruction, previous: ComponentInstruction) => {
let injector: Injector = appInjector(); // get the stored reference to the injector
let router: Router = injector.get(Router);
let http: Http = injector.get(Http);
// return a boolean or a promise that resolves a boolean
return new Promise((resolve) => {
http
.get('/orma/api/v1/users/current')
.map(res => res.json())
.subscribe(
(userData) => {
console.log('ok, we got user data', userData);
resolve(true);
},
(err) => {
console.log('authentication failed, redirecting');
router.navigate(['/login']);
resolve(false);
});
});
};
有人可以给我一个在新的 angular2 路由器中做这一切的例子吗?
谢谢