【问题标题】:Angular2 get current route object like ng-route's toState paramAngular2 获取当前路由对象,如 ng-route 的 toState 参数
【发布时间】: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


    【解决方案1】:

    我认为您可以扩展RouterOutlet 类以全局方式拦截路由激活:

    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);
        }
      }
    }
    

    你需要这样配置路由:

    {
      path: '/result/:searchString',
      name: 'Result',
      component: ResultComponent,
      data: {
        needAuth: true
      }
    }
    

    查看这些链接了解更多详情:

    【讨论】:

    • 我无法让它工作。但找到了另一个解决方案。无论如何谢谢:)
    • 有什么问题?
    【解决方案2】:

    代替

    needAuth: true
    

    RouteConfig使用

    data: {needAuth: true}
    

    然后注入它

    constructor(private routeData: RouteData) {
      if(routeData.needAuth) {
        ...
      }
    }
    

    【讨论】:

    • 感谢回复,我试过了,但它只返回路径参数,我需要整个对象来捕获自定义的 'key':'value' 比如需要验证:true
    • 谢谢,但我不会工作,因为 RouteData 仅用于设置子路由的参数。我找到了另一个修复方法,但它很难看。
    • 我明白了。我不觉得它太丑了。
    猜你喜欢
    • 2016-04-01
    • 1970-01-01
    • 2021-06-30
    • 2017-03-23
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2018-05-18
    相关资源
    最近更新 更多