首页 > 解决方案 > 如何将 params.get('brand') 分配给以下函数中的变量?

问题描述

我正在尝试将结果分配给map 方法params.get('brand')中的变量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)
    })    
  }

标签: angular

解决方案


一个工作示例链接会有所帮助。

您可以使用rxjs'tap运算符来获得副作用。

例如

 this.products$ = this.route.paramMap.pipe(
      tap((params:ParamMap) => this.brandName = params.get('brand')),
      switchMap((params:ParamMap) => 
         this.dataService.getProductsByBrand(params.get('brand')))      
    )

推荐阅读