代码之家  ›  专栏  ›  技术社区  ›  koque

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

  •  0
  • koque  · 技术社区  · 7 年前

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

    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 回复  |  直到 7 年前
        1
  •  0
  •   joshvito    7 年前

    一个有效的示例链接会有帮助。

    你可以用 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')))      
        )
    
        2
  •  -1
  •   Sunil Singh    7 年前

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

    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)
           }) 
        });
    
    推荐文章