2

There are still some questions and really now infos about the new angular2 router.

First off, there are no 'name' in the new @Routes, so there should be always relative URL paths, and that's okay.

But how can I check which is my activated route?? In the deprecated router I did this:

let instruction = this.router.generate(['Login']);
let isLoginRoute =  this.router.isRouteActive(instruction);

the other thing is, Which is the best way to check if user has permissions to navigate to route. What is the best practice for it, until now I did something like that:

@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);
                });
    });
};

can someone gives me an example for do all this in new angular2 router?

thx

Michael Burger
  • 643
  • 1
  • 9
  • 17
  • What do you mean by " so there should be always relative URL paths, and that's okay." You can also use absolute paths. I'm not sure about the current state of router lifecycle methods, not all are implemented in RC.1 yet. Using a custom router-outlet was also a common strategy to check permissions with routing. AFAIK there was an open issue with checking active route which should be fixed already. – Günter Zöchbauer May 23 '16 at 18:14
  • In the last ngConf on the speak for new router they explained that using relative urls is helping scalling and reusing code, nothing more. It would be nice to have more infos in general on how to do this, I think 75% of the programmers need my use case – Michael Burger May 24 '16 at 12:53

0 Answers0