【问题标题】:How to make data in var as global using angular2 or typescript如何使用 angular2 或 typescript 将 var 中的数据设为全局
【发布时间】:2018-05-14 00:46:15
【问题描述】:

我有一个功能,我可以在控制台中获取 id,但我无法在 Apiservice 中获取它,请谁能帮我做。

TS:

  deleteProduct(index,product) {
    var token = this.auth.getAccessTokenId();
    this.products.splice(index, 1);
    var del = this.products.splice(index, 1);
    del.map(function(product) {
    var Id = product.id;
    console.log(Id);
  })
    this.ApiService
     .deleteProducts(this.Id,token)
     .subscribe(
       products => {
         console.log(products);
         this.products = products;
       }, error => {
         //console.log(error);
       })
  } 

直到 console.log(Id);它工作正常,但它不会出现在 ApiService 中

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    你可以使用下面的sn-p:

     deleteProduct(index) { 
    var token = this.auth.getAccessTokenId(); 
    var deletedProduct = this.products[index]; 
    this.products.splice(index, 1); 
    this.ApiService 
    .deleteProducts(deletedProduct.id,token) 
    .subscribe( 
    products => { 
    console.log(products); 
    this.products = products; 
    }, error => { 
    //console.log(error); 
    }) 
    }
    

    【讨论】:

    • 例外:无法设置未定义的属性 'Id' 我明白了
    • 您在哪一行收到此异常?
    • var id = product.id; this.Id = 产品.id; console.log(this.Id);
    • 为什么要在方法中创建一个新的 id 变量?
    • 从哪里调用“deleteProduct”方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多