javascript - 将括号内的字符串传递给另一个字符串

标签 javascript angular typescript custom-element

我的问题与 JS 和自定义元素有关。

我有以下代码:

let product=this.getProduct(message['productname']);

我不清楚的是message是上面的一个字符串...

将括号内的字符串(即 ['productname'])传递给另一个字符串(即 message)的结果是什么?

在这里:

消息['productname']

这个符号/语法的名称是什么?

完整列表如下:

import { Component, TemplateRef, Renderer2, OnDestroy, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';


export class Product {
  public productname:string;
  public code: string;
  public cartprice: number;
  public price: number;
  public available: number;
  public qty: number;
  constructor(){

  }
}

@Component({
  selector: 'product-cart',
  templateUrl: './productcart.component.html',
  styleUrls: ['./productcart.component.css'],
  encapsulation: ViewEncapsulation.Emulated
})
export class ProductCartComponent implements OnInit, OnDestroy {

  public productlist : Product[]; 
  public totalprice=0;
  public ngOnInit(): void {
    this.productlist=[];
  }
  constructor(private renderer: Renderer2) {
  }
  @Input()
  set message(message: string) {
    this.processMessage(message);
  }
  get message(): string { return this._message; }
  _message: string;

  processMessage(message) {
    let product=this.getProduct(message['productname']);
    if(product !== undefined) {
      product.qty=product.qty + 1;
      product.cartprice=product.cartprice+message['price'];
      this.totalprice=this.totalprice+message['price'];
     } else if(message !== "" && message !== undefined) {
      product = new Product();
      product.qty=1;
      product.price=(message['price']!== undefined)?message['price']:0;
      product.productname=(message['productname'] !== undefined)?message['productname']:"";
      product.available=(message['available'] !== undefined)?message['available']:0;
      product.code=(message['code'] !== undefined)?message['code']:"";
      product.cartprice=(message['price'] !== undefined)?message['price']:0;
      this.productlist.push(product);
      this.totalprice=this.totalprice+product.price;
    }

  }

  getProduct(productname) : Product {
    let productObj=undefined;
    for(let product of this.productlist) {
      if(product.productname === productname) {
        productObj=product; 
        break;
      }
  }
  return productObj;
  }
  ngOnDestroy() {
  }
  increment(product) {
    if(product.qty >= 0 && product.qty < product.available) {
      product.qty =product.qty + 1;
      product.cartprice = product.cartprice + product.price;
      this.totalprice = this.totalprice + product.price;
      this.sendMessageToProductView(product);
    }
  }

  decrement(product) {
    if(product.qty > 0 && product.qty <= product.available) {
      product.qty =product.qty - 1;
      product.cartprice = product.cartprice - product.price;
      this.totalprice = this.totalprice - product.price;
      this.sendMessageToProductView(product);
    }
  }
  sendMessageToProductView(product) {
    const productviewele = document.querySelector('product-view');
    if(productviewele != null) {
      productviewele['message']=product;
    }
  }
}

编辑:我可以确认消息是对象类型而不是字符串类型。正如 Jonas 所暗示的那样,setter 参数输入错误。

最佳答案

message['productname'] 与 message.productname 相同,如果 message 是字符串,则它将是未定义的。如果消息是一个对象,它将是 productname 属性。

所以代码:

product.productname=(message['productname'] !== undefined)?message['productname']:"";

如果 message 是一个对象,似乎得到产品名称,如果它是一个字符串,则似乎得到 productname。

关于javascript - 将括号内的字符串传递给另一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51180057/

相关文章:

javascript - php文件中的uglifyjs javascript

javascript - 使用解构 JavaScript 从数组对象中获取数组值

javascript - 为什么 AJAX 调用不仅仅在 iOS 上工作,在其他地方都工作得很好?

angular - 如何在 Angular 2 中显示隐藏的(星号 **)代替密码

typescript - 在 tslint.json 的配置中包含什么?

javascript - Sapper 中的 Axios 请求两次?

javascript - Angular 2 测试应用程序卡在加载中

css - 如何覆盖 ng2 pdf 查看器 css

node.js - 将 inversifyJS 配置文件拆分为多个文件

javascript - 如何检查对象中的值是否是原始值?