【发布时间】:2017-09-19 14:41:07
【问题描述】:
@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'])
}
});
});
【问题讨论】:
标签: angularjs typescript