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

离子2/3可变访问

  •  1
  • cdh429  · 技术社区  · 7 年前

    我正在使用下面的教程构建应用内购买。

    https://github.com/thielCole/ionic-iap2/blob/master/src/pages/about/about.ts

    我正在尝试使googleProductID等于GET调用中的googleProductID(在“content”数组中)

    public product: any = {
       name: 'Quiz',
       appleProductID: '1234',
       googleProductID: this.content[0]['googleProductID'],
     };
    

    我一直收到这个错误。。。

    core.es5.js:1020 ERROR Error: Uncaught (in promise): TypeError: Cannot read property '0' of undefined
    TypeError: Cannot read property '0' of undefined
    

    GET调用中的所有内容都匹配-我是有语法错误还是无法在此处进行调用?

    ***** 编辑 *******

    我正在将数据从一个页面传递到另一个页面。我正在使用IonViewWillLoad从GET调用传递内容。

    ionViewWillLoad() {
          this.content = this.navParams.get('content');
          this.gameGear = this.navParams.get('gameGear');
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Andrew Radulescu    7 年前

    完成工作后,请查看Javascript吊装 here 了解解释器如何读取/转换代码。

    基本上,首先声明变量,然后初始化,最后将声明移到顶部 ionViewDidLoad 调用(示例)。

    public product: any = {
        name: 'Quiz',
        appleProductID: '1234',
        googleProductID: undefined
    };
    

    从navParams检索时,可以更新对象

    ionViewWillLoad() {
          this.content = this.navParams.get('content');
          // Now you have the value 
          this.product.googleProductId = this.content[0]['googleProductID'],
          this.gameGear = this.navParams.get('gameGear');
    }