In angular 2 the RouterOutlet seems to have changed with the new library for @router does anyone know what to?
I'm using angular 2 with typescript.
My code below redirects the user to home view if they are not logged in when trying to reach a page that requires login.
Since I've updated my library for @angular/router. it seems to stop working and the console doesnt give me a readable message but I'm assuming the RouterOutlet has changed or the ComponentInstruction.
These are my angular2 dependencies:
@angular/common": "2.0.0-rc.1",
"@angular/compiler": "2.0.0-rc.1",
"@angular/core": "2.0.0-rc.1",
"@angular/http": "2.0.0-rc.1",
"@angular/platform-browser": "2.0.0-rc.1",
"@angular/platform-browser-dynamic": "2.0.0-rc.1",
"@angular/router": "2.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.1",
"@angular/upgrade": "2.0.0-rc.1",
I've added my code below:
import {Directive, Attribute, ElementRef, DynamicComponentLoader} from '@angular2/core'; import {Router, RouterOutlet, ComponentInstruction} from '@angular2/router';
import {Environment} from './models/environment/environment';
@Directive({
selector: 'router-outlet'
})
export class LoggedInRouterOutlet extends RouterOutlet {
publicRoutes: any;
private parentRouter: Router;
environment: Environment;
constructor(_elementRef: ElementRef, _loader: DynamicComponentLoader, _parentRouter: Router, @Attribute('name') nameAttr: string, _environment: Environment) {
super(_elementRef, _loader, _parentRouter, nameAttr);
this.parentRouter = _parentRouter;
this.environment = _environment;
this.publicRoutes = [
'home',
'contact',
'forgotpassword',
'socialRegister',
'login',
'register/',
'register/:id',
'blog/:slug',
'blog',
'sitemap',
'newJobs',
'newJob/:slug'
];
}
activate(instruction: ComponentInstruction) {
var ref = new Firebase(this.environment.firebaseUrl);
var authToken = ref.getAuth();
var isPublicRoute = this.publicRoutes.indexOf(instruction.urlPath) > -1;
var registerRouter = instruction.urlPath.substring(0, 8) === "register";
var socialRegister = instruction.urlPath.substring(0, 14) === "socialRegister";
var blogRouter = instruction.urlPath.substring(0, 4) === "blog";
var newJobRouter = instruction.urlPath.substring(0, 6) === "newJob";
if(authToken == null && !isPublicRoute && !registerRouter && !socialRegister && !blogRouter && !newJobRouter)
this.parentRouter.navigate(['/Home']);
return super.activate(instruction);
}
}
@angular/routeranyway. It will be replaced soon (again). Rather stick with@angular/router-deprecated. - Günter Zöchbauer