2016-06-07 62 views
2

我需要捕获查询字符串,但是我得到错误。我使用的代码很简单,但是在浏览器中失败了。RouteParams和查询字符串

@Component({ 
    selector: 'body', 
    viewProviders: [ ROUTER_PROVIDERS], 
    directives: [ CORE_DIRECTIVES, ROUTER_DIRECTIVES], 
    template: '<p>the param is {{foo}}</p>' 
}) 
export class app { 
    constructor(params: RouteParams) { 
      // http://local.../?myText=Hello  
      this.foo = params.get('myText'); 
     } 
} 

documentation对这类事情不是很清楚。它完全没有,只是一个null

+0

您使用的是过时路由器包? –

+0

我没有使用该软件包。我使用最后一个软件包,是2.0.0-rc.1 –

回答

3

在角RC1路由器,你需要有你的组件实现OnActivate接口来获取路由参数:

import { ROUTER_DIRECTIVES, Router, OnActivate, RouteSegment } from '@angular/router'; 

Component({ 
    selector: 'body', 
    providers: [ ROUTER_PROVIDERS], 
    directives: [ CORE_DIRECTIVES, ROUTER_DIRECTIVES], 
    template: '<p>the param is {{foo}}</p>' 
}) 
export class app implements OnActivate { 
    constructor() { } 

    routerOnActivate(currSegment: RouteSegment) { 
     this.foo = currSegment.getParam('myText'), 
} 
2

我认为你必须更换viewProvidersproviders或者更好的是,如果你在引导你的应用程序是这样的时间提供ROUTER_PROVIDERS: -

bootstrap(AppComponent, [ROUTER_PROVIDERS]); 

可以帮助你,如果你仍然有任何问题,请更新您的问题与错误信息或更好,如果你提供任何重击。