新的Angular2 RC路由器

时间:2016-05-23 13:10:05

标签: angular router angular2-router

现在还有一些关于新的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路由器中做这一切吗?

THX

0 个答案:

没有答案