2017-07-28 120 views
-1

我需要下一個URL發送到組件:角GET查詢參數

http://localhost:3000/interactive/malenkoe_obyavlenie?device_type=phone

我想未來:

@NgModule({ 
    imports: [ 
    RouterModule.forChild([ 
    { 
     path: ':id?device_type', 
     component: InteractivePreviewComponent 
    } 
    ]), 
    CommonModule 
    ], 
    declarations: [InteractivePreviewComponent] 
}) 

,但它不是作品。 path: ':id/:device_type*', - 在這種情況下,我曾模塊,但它帶來的另一個URL格式 http://localhost:3000/interactive/malenkoe_obyavlenie/phone

UPD:我的目標是獲得「queryParams」。

+2

你不立足*路由*上的查詢參數,它們是額外的信息你從組件中的激活路由中獲得。見例如https://angular.io/guide/router#activatedroute-the-one-stop-shop-for-route-information – jonrsharpe

+0

在AngularJS中,這非常簡單:'$ stateProvider .state('incidents-list',{ url :'/事件?頁面&perPage&on&狀態})',我認爲在Angular2中它也必須起作用。 – kunashir

+0

[Angular 2 Routing based on query params]可能的副本(https://stackoverflow.com/questions/42299753/angular-2-routing-based-on-query-params) – jonrsharpe

回答

0

我發現queryParams在ActivatedRoute不同的領域:

this.routeParamsSubscription = this.route.params.subscribe(params => { 
//get id from params 
if(params['id']) { 
    this.slug = params['id'] 
    //subscribing to change queryParams 
    this.routeParamsSubscription = this.route.queryParams.subscribe(params => { 
     // get query params this! 
     this.init(this.slug, params['device_type']) 
    }) 
    } 
}) 

這個話題很好的教程: https://angular-2-training-book.rangle.io/handout/routing/query_params.html