Angularjs Angular2获取当前路由对象,如ng route';s toState参数
我有一个这样的配置,我怎样才能检测到当前的路由对象像Angularjs Angular2获取当前路由对象,如ng route';s toState参数,angularjs,typescript,Angularjs,Typescript,我有一个这样的配置,我怎样才能检测到当前的路由对象像 @RouteConfig([ { path: '/login', name: 'Login', component: LoginComponent }, { path: '/search', name: 'Search', component: SearchComponent, needAuth: true }, { path: '/result/:searchString',
@RouteConfig([
{
path: '/login',
name: 'Login',
component: LoginComponent
},
{
path: '/search',
name: 'Search',
component: SearchComponent,
needAuth: true
},
{
path: '/result/:searchString',
name: 'Result',
component: ResultComponent,
needAuth: true
},
{path: '/**', redirectTo: ['Login']}
])
用于用户未登录时的路由限制
我想这样做
{
path: '/result/:searchString',
name: 'Result',
component: ResultComponent,
needAuth: true
}
}
欢迎各种帮助
**************更新********************
我找到了这个解决方案
export class AppComponent {
constructor(private _authService:AuthenticationService) {
if (this._authService.getUser() != null && CURRENT ROUTE OBJECT's needAuth PROPERTY is TRUE) {
this._router.navigate(['Search'])
} else {
this._router.navigate(['Login'])
}
}
而不是
this._router.subscribe((url) => {
this._router.recognize(url).then((instruction) => {
if(this._authService.getUser() == null && instruction.component.routeData.data.needAuth) {
this._router.navigate(['Login'])
}
if(this._authService.getUser() !=null && !instruction.component.routeData.data.needAuth) {
this._router.navigate(['Search'])
}
});
});
在RouteConfig
中使用
needAuth: true
然后注射它
data: {needAuth: true}
而不是
this._router.subscribe((url) => {
this._router.recognize(url).then((instruction) => {
if(this._authService.getUser() == null && instruction.component.routeData.data.needAuth) {
this._router.navigate(['Login'])
}
if(this._authService.getUser() !=null && !instruction.component.routeData.data.needAuth) {
this._router.navigate(['Search'])
}
});
});
在RouteConfig
中使用
needAuth: true
然后注射它
data: {needAuth: true}
我认为您可以扩展
RouterOutlet
类,以全局方式拦截路由激活:
constructor(private routeData: RouteData) {
if(routeData.needAuth) {
...
}
}
您需要通过以下方式配置路由:
import {Directive} from 'angular2/core';
import {Router, RouterOutlet, ComponentInstruction} from 'angular2/router';
@Directive({
selector: 'router-outlet'
})
export class MyOwnRouterOutlet extends RouterOutlet {
(...)
activate(oldInstruction: ComponentInstruction) {
var url = this.parentRouter.lastNavigationAttempt;
console.log('attemping to nav');
if (oldInstruction.routeData.needAuth && !this.authService.loggedIn){
var newInstruction = new ComponentInstruction('Login', [], new RouteData(), Login, false, 1);
return super.activate(newInstruction);
} else {
return super.activate(oldInstruction);
}
}
}
有关更多详细信息,请参阅以下链接:
RouterOutlet
类,以全局方式拦截路由激活:
constructor(private routeData: RouteData) {
if(routeData.needAuth) {
...
}
}
您需要通过以下方式配置路由:
import {Directive} from 'angular2/core';
import {Router, RouterOutlet, ComponentInstruction} from 'angular2/router';
@Directive({
selector: 'router-outlet'
})
export class MyOwnRouterOutlet extends RouterOutlet {
(...)
activate(oldInstruction: ComponentInstruction) {
var url = this.parentRouter.lastNavigationAttempt;
console.log('attemping to nav');
if (oldInstruction.routeData.needAuth && !this.authService.loggedIn){
var newInstruction = new ComponentInstruction('Login', [], new RouteData(), Login, false, 1);
return super.activate(newInstruction);
} else {
return super.activate(oldInstruction);
}
}
}
有关更多详细信息,请参阅以下链接: