如何在以下函数中将params.get('brand')分配给变量?

时间:2018-10-11 16:12:38

标签: angular

我正在尝试将params.get('brand')的结果分配给map方法中的变量brandName,但是它不起作用。我该怎么办?

private getProducts() {
    this.products$ = this.route.paramMap.pipe(
      map((params:ParamMap) => this.brandName = params.get('brand')),
      switchMap((params:ParamMap) => 
         this.dataService.getProductsByBrand(params.get('brand')))      
    )
    this.products$.subscribe(products => {
      this.setProducts(products);
      console.log('products', this.products)
    })    
  }

2 个答案:

答案 0 :(得分:0)

有效的示例链接会有所帮助。

您可以使用rxjs的{​​{1}}运算符来产生副作用。

例如

tap

答案 1 :(得分:-1)

尝试以下代码段,并检查是否适合您

constructor(private activatedRoute: ActivatedRoute) {

}

private getProducts() {
    this.activatedRoute.queryParams.subscribe(params => {
         this.brandName = params['brand']; //read the param brand from route
         this.dataService.getProductsByBrand(this.brandName).subscribe(products => {
           this.setProducts(products);
           console.log('products', this.products)
       }) 
    });