如何将第二个路由参数传递给angular 2服务模块?

时间:2016-09-03 16:18:29

标签: angular typescript angular2-routing

我是角度2的新手,并希望了解如何将多个参数传递给角度的服务模块。例如,这是我通过路由器链接传递给服务的第一个参数。

家庭component.html

<a href="#" [routerLink]="['/product', product.slug]">

映射到下面在app.routes.ts中定义的路由并加载ProductOverviewComponent。

const appRoutes: Routes = [
{ path: 'product/:slug', component: ProductOverviewComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: '', component: HomeComponent, canActivate: [AuthGuard] },

// otherwise redirect to home
{ path: '**', redirectTo: '' }
];

product-overview.component.ts调用getProductOverview()方法。

ngOnInit() {
  this.sub = this.route.params.subscribe(params => {
    let slug = params['slug'];
    console.log('getting product with slug: ', slug);
    this.productoverviewservice.getProductOverview(slug)
    .subscribe(p => this.product = p);
  });
}

从product-overview.service.ts

调用getProductOverview方法
getProductOverview(slug: string): Observable<Course> {
    let product$ = this.http
        .get(`${this.baseUrl}${slug}/` , options)
        .map((response: Response) => response.json());
}

我的问题是如何将第二个路由参数传递给函数getProductDetail?

例如:

getProductDetail(slug: string): Observable<Course> {
    let productDetail$ = this.http
        .get(`${this.baseUrl}${slug}/${slug2}` , options)
        .map((response: Response) => response.json());
}

非常感谢任何输入。

1 个答案:

答案 0 :(得分:1)

你应该有这样的路由:

{path: 'product/:slug/:slug2', component: ProductOverviewComponent, canActivate: [AuthGuard] }])

它应该有效!