我对组件进行了以下设置。
Boot.ts
|__Home.ts
|__Aboutus.ts
|__contactus.ts
引导.ts
directives:[AuthOutlet,HomeCmp,ROUTER_DIRECTIVES,ClientCmp,AboutUsCmp,Login],
template: `
<auth-outlet></auth-outlet>
`
@RouteConfig([
{path:'/Home', name: 'Home', component: HomeCmp, useAsDefault: true}
{path:'/AboutUs', name: 'AboutUs', component: AboutUsCmp}
{path:'/Clients', name: 'Client', component: ClientCmp}
{path:'/Login', name: 'Login', component: Login}
])
authOutlet.ts
import {Directive, Attribute, DynamicComponentLoader, ElementRef} from 'angular2/core';
import {RouterOutlet, Router, ComponentInstruction, RouteData } from 'angular2/router';
import {AuthService} from 'Angular/src/authService.ts';
import {Login} from 'Angular/src/Login/login.ts';
@Directive({
selector: 'auth-outlet'
})
export class AuthOutlet extends RouterOutlet {
publicRoutes: any;
private parentRouter: Router;
private authService: AuthService;
constructor(_elementRef: ElementRef, _loader: DynamicComponentLoader, _parentRouter: Router,
@Attribute('name') nameAttr: string, _authService: AuthService) {
super(_elementRef, _loader, _parentRouter, nameAttr);
this.parentRouter = _parentRouter;
this.authService = _authService;
this.publicRoutes = {
'/AboutUs': true,
'/Home':true
};
}
activate(oldInstruction: ComponentInstruction) {
console.log(this.parentRouter);
// here I get this.parentRouter object.
var url = this.parentRouter.lastNavigationAttempt;
________________________________________________________________
here I get blank url because lastNavigationAttempt is always " " (blank).
___________________________________________________________
I want to have some value in url so further I can do something.
________________________________________________________________
I can't figure out what is the problem and why????Is anything missing?
_______________________________________________________________
console.log('redirecting to ' + url);
var user=JSON.parse(localStorage.getItem('UserData');
console.log('User Data');
console.log(user);
console.log(this.publicRoutes[url]);
if(user!=null)
{
if (!this.publicRoutes[url] && !user.loggedIn){
var newInstruction = new ComponentInstruction('Login', [], new RouteData(), Login, false, 1);
console.log(newInstruction);
return super.activate(newInstruction);
} else {
console.log(oldInstruction);
return super.activate(oldInstruction);
}
}
else
{
console.log('Outlet - bye bye logout');
var newInstruction = new ComponentInstruction('Login', [], new RouteData(), Login, false, 1);
console.log(newInstruction);
return super.activate(newInstruction
}
}
}
如果我开始获取 url 值,那么我可以在IF
下面某处使用的条件下使用它。还有其他方法吗?或者我应该怎么做才能获得那个 url 值?