将参数传递给 ngOnInit 中的方法甚至不调用服务,不记录任何内容

Passing a parameter to a method inside ngOnInit doesnt even call a Service, log nothing

在我制作的上一个组件页面上,一旦您单击一个按钮,它就会使用导航将您带到其他组件。考虑到我必须写两个参数,一个用于当前路径,第二个用于下一个,当我尝试订阅它时,它给了我一个包含 2 个元素的对象。

onSelect(subCategory) {
    this.router.navigateByUrl(
      `web-shop/categories/${this.type}/${subCategory.nextType}`
    );}

在 ngOnInit 中,我捕获了该对象,将其更改为数组并将其值赋给另一个变量。然后我取了数组的第二个元素。当我在代码中调用 this.products 中的一个方法,过滤它,然后记录它时,我在 console.log 中什么也得不到,即使我传递了我需要的实际值,我检查该值是否是我需要的。但出于某种原因,对于 this.products,我没有得到 console.log 的任何结果,甚至连数组或值都没有。

import { Component, OnInit } from "@angular/core";
import { Router, ActivatedRoute } from "@angular/router";
import { Subscription } from "rxjs";
import { SingleProductModel } from "./../../Models/webshopModels/single-product.model";
import { SingleProductService } from "./../../Services/webshopServices/single-product.service";

@Component({
  selector: "app-products",
  templateUrl: "./products.component.html",
  styleUrls: ["./products.component.scss"],
})
export class ProductsComponent implements OnInit {
  type: string;
  products: SingleProductModel[];
  s: string;
  type1: Array<any>;
  constructor(
    private productsService: SingleProductService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.route.params.subscribe((data) => {
      this.type1 = Object.values(data);
      console.log(this.type1);
      this.type = this.type1[1];
      console.log(this.type);
      this.products = this.productsService
        .getCategoryProducts()
        .filter((a) => (a.type = this.type));
      console.log(this.products);
    });
  }
}

我什至尝试在服务本身内部对其进行过滤。我发送的示例中没有。

  getCategoryProducts() {
    return this.product.slice();
  }

在过滤语句中,你实际上使用了=它的赋值而不是比较。

而不是 = 使用 =====

    this.productsService
        .getCategoryProducts()
        .filter((a) => (a.type === this.type))
        .subscribe(products => this.products = products);

所以我解决了这个问题。问题是,我在模型中有一个方法,导致我的程序无法运行和中断,因为我从产品服务内部的产品评论服务调用了一个函数,而这个破坏代码的方法在产品评论模型中.我尝试了一些东西,但它很愚蠢。感谢大家的帮助。