2016-06-07 106 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]); 

可以幫助你,如果你仍然有任何問題,請更新您的問題與錯誤信息或更好,如果你提供任何重擊。