2017-07-28 4 views
-1

ich nächste URL Komponente senden müssen:Angular get Abfrage params

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

Ich versuchte weiter:

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

aber es ist nicht funktioniert. path: ':id/:device_type*', - in diesem Fall habe ich Modul gearbeitet, aber es bringt eine andere URL-Format http://localhost:3000/interactive/malenkoe_obyavlenie/phone

UPD: Mein Ziel ist es 'queryParams' bekommen.

+2

Sie können als * Routing-Basis * auf den Abfrageparametern, sie sind zusätzliche Informationen Sie erhalten von der aktivierten Route in der Komponente. Siehe z.B. https://angular.io/guide/router#activedroute-the-one-stop-shop-for-route-information – jonrsharpe

+0

In AngularJS war das ziemlich einfach '$ stateProvider .state ('Incidents-Liste', { url : '/ Incidents? page & perPage & on & state}) 'und ich dachte in Angular2 muss es auch funktionieren. – kunashir

+0

Mögliches Duplikat von [Angular 2 Routing basierend auf Abfrageparametern] (https://stackoverflow.com/questions/42299753/angular-2-routing-based-on-query-params) – jonrsharpe

Antwort

0

fand ich queryParams in verschiedenen Bereichen 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']) 
    }) 
    } 
}) 

gutes Tutorial zu diesem Thema: https://angular-2-training-book.rangle.io/handout/routing/query_params.html