2017-07-28 68 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