typescript - Angular 2 - Redirct a user to homepage if they try to access a logged in page. (I need it not to hit the components constructor) -
angular 2 - redirct user homepage if try access logged in page. (i need not hit components constructor)
i'm using typescript , angular2.
currently works if have no calls services in components constructor.
currently i'm using doing (but keeps hitting internal components constructor still , causing errors):
import {directive, attribute, elementref, dynamiccomponentloader} 'angular2/core'; import {router, routeroutlet, componentinstruction} 'angular2/router'; @directive({ selector: 'router-outlet' }) export class loggedinrouteroutlet extends routeroutlet { publicroutes: any; private parentrouter: router; constructor(_elementref: elementref, _loader: dynamiccomponentloader, _parentrouter: router, @attribute('name') nameattr: string) { super(_elementref, _loader, _parentrouter, nameattr); this.parentrouter = _parentrouter; this.publicroutes = { '/home': true }; } activate(instruction: componentinstruction) { console.log("here"); this.parentrouter.navigate(['/home']); return super.activate(instruction); } }
it works if internal page doesn't have in constructor.
what need @canactivate directive in front of protected component.
https://angular.io/docs/ts/latest/api/router/canactivate-decorator.html
@canactivate(() => checkauth()) export class protectedcmp {}
consider plunker (protected.component.ts )
Comments
Post a Comment