繁体   English   中英

如何调用 function class javascript

[英]how to call a function class javascript

我想创建一个带有私有参数和函数的 class 来访问我想要的数据。 你可以看到这个:

export class Product {
  private name: string;
  private type: string;
  private longDetail: string;
  private shortDetail: string;
  private stock: number;
  private price: number;
  private linkImage: string;
  private id: number;

  constructor(
    name: string,
    type: string,
    longDetail: string,
    shortDetail: string,
    stock: number,
    price: number,
    linkImage: string,
    id: number
  ) {
    this.name = name;
    this.type = type;
    this.longDetail = longDetail;
    this.shortDetail = shortDetail;
    this.stock = stock;
    this.price = price;
    this.linkImage = linkImage;
    this.id = id;
  }

  getName(): string {
    return this.name;
  }
  getType(): string {
    return this.type;
  }
  getLongDetail(): string {
    return this.longDetail;
  }
  getShortDetail(): string {
    return this.shortDetail;
  }
  getStock(): number {
    return this.stock;
  }
  getPrice(): number {
    return this.price;
  }
  getLinkImage(): string {
    return this.linkImage;
  }
  getId(): number {
    return this.id;
  }
}

当我想在组件中调用 function 时,我被告知:

ProductListComponent.html:15 错误类型错误:newProduct.getName 不是 function

你有解决方案吗? 非常感谢您提前!

编辑:

这是前端点击后调用的代码

addProductBasket(newProduct: Product) {
  const newClientBasket = this.createNewClientBasketWithAdd(
    this.clientBasket.getValue(),
    newProduct
  )
  this.clientBasket.next(newClientBasket)
  console.log(newClientBasket)
}

private createNewClientBasketWithAdd(
  oldClientBasket: BasketProduct[],
  newProduct: Product
): BasketProduct[] {
  const found = oldClientBasket.find((product) => {
    if (product.getId() === newProduct.getId()) {
      product.addOneProduct()
    }
  })

  if (found === undefined) {
    console.log(newProduct.getName())
    oldClientBasket.push(
      new BasketProduct(
        newProduct.getName(),
        newProduct.getType(),
        newProduct.getLongDetail(),
        newProduct.getShortDetail(),
        newProduct.getStock(),
        newProduct.getPrice(),
        newProduct.getLinkImage(),
        newProduct.getId()
      )
    )
  }
  return oldClientBasket
}

获取数据是我的 apiservice

export class ApiService {
  private dataApi: BehaviorSubject<Product[]> = new BehaviorSubject<Product[]>([]);

  constructor(private http: HttpClient) {
    this.getDataFromApi();
  }
  private getDataFromApi(){
    this.http
      .get<Product[]>("../../assets/data.json")
      .toPromise()
      .then((data) => this.dataApi.next(data));
  }
  public getData():Observable<Product[]>{
    return this.dataApi.asObservable();
  }
}

在访问其方法之前,您应该拥有Product class 的实例。

var newProduct = new Product();
newProduct.getName();

在大卫帮助评论之后,我明白我必须实例化我在 http 客户端收到的数据。

然后我修改了构造函数和我的客户端 http 得到

  constructor(obj: any) {
    Object.assign(this, obj);
  }

private getDataFromApi(){
    this.http
      .get<Product[]>("../../assets/data.json").pipe()
      .toPromise()
      .then((data) => {
        const productList = data.map(product => new Product(product));
        this.dataApi.next(productList)});
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM