Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular2获取当前路由对象,如ng route';s toState参数_Angularjs_Typescript - Fatal编程技术网

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);
    }
  }
}
有关更多详细信息,请参阅以下链接:


感谢您的回复,我尝试了,但它只返回路径参数,我需要整个对象来捕获自定义的“键”:“值”,如needAuth:TrueThance,但我不会工作,因为RouteData仅用于设置子路由的参数。我找到了另一种修复方法,但很难看。我明白了。我不认为它太难看。谢谢你的回复,我试过了,但它只返回路径参数,我需要整个对象来捕获自定义的“键”:“值”,如needAuth:TrueThance,但我不会工作,因为RouteData只用于设置子路由的参数。我找到了另一种修复方法,但很难看。我明白了。我不认为它太难看。我没法让它工作。但找到了另一个解决办法。无论如何谢谢:)怎么了?我没法让它工作。但找到了另一个解决办法。无论如何,谢谢:)有什么问题吗?