Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Polymer 聚合web应用程序元素空指针异常_Polymer_Progressive Web Apps - Fatal编程技术网

Polymer 聚合web应用程序元素空指针异常

Polymer 聚合web应用程序元素空指针异常,polymer,progressive-web-apps,Polymer,Progressive Web Apps,我是使用iron ajax的用户iron list,但在将元素推送到iron list时,在ajax响应之后,会给出null exception=“无法读取未定义的属性”\u list” 聚合物({ 是‘我的家’, 就绪:函数(){ 这是$.\u ajax.generateRequest(); }, onResponse:函数(e){ var people=e.detail.response.data; 人员。forEach(功能(人员){ 此.$.\u列表推送('items',person)

我是使用iron ajax的用户iron list,但在将元素推送到iron list时,在ajax响应之后,会给出null exception=“无法读取未定义的属性”\u list”


聚合物({
是‘我的家’,
就绪:函数(){
这是$.\u ajax.generateRequest();
},
onResponse:函数(e){
var people=e.detail.response.data;
人员。forEach(功能(人员){
此.$.\u列表推送('items',person);
});
//清除较低的阈值,这样我们可以在用户再次向下滚动时加载更多数据。
这个._scrollTheshold.clearTriggers();
}
});
这是我的铁名单的html代码

 <iron-list id="_list" items="[[data.data]]" as="item">
        <template>

            <div style="margin-top: 0px">

                <!--Card with header image http://placehold.it/350x150/FFC107/000000-->
                <paper-card>

                    <div class="card-content">
                        <h1 class="feed_title">[[item.heading]]</h1>

                        <p class="feed_description">[[item.subheading]]</p>
                    </div>
                    <div class="card-actions">
                        <paper-button class="button-blue">
                            Share
                        </paper-button>
                        <paper-button class="button-blue">
                            Explore
                        </paper-button>
                    </div>
                </paper-card>
            </div>
        </template>
    </iron-list>

[[项目.标题]]

[[项目子目]]

分享 探索
forEach()
函数表达式中,
引用的是
窗口
对象,而不是聚合对象。(如果在此处使用,
将是
未定义的
,但您似乎不是这样。)

解决此问题的几个选项:

  • 选项1:使用,它不绑定自己的
    (假设您可以使用ES6):

  • 选项2:使用,无需回调(假设您可以使用ES6):

  • 选项3:用于一次推送所有阵列项(假设ES6对您可用):

  • 选项4:传入对聚合物对象的引用:

    onResponse: function (e) {
      var self = this;
      var people = e.detail.response.data;
      people.forEach(function (person) {
        self.$._list.push('items', person);
      });
      ...
    }
    
  • 选项5:传入对列表对象的引用:

    onResponse: function (e) {
      var list = this.$._list;
      var people = e.detail.response.data;
      people.forEach(function (person) {
        list.push('items', person);
      });
      ...
    }
    
  • 选项6:在函数表达式中显式绑定此:

    onResponse: function (e) {
      var people = e.detail.response.data;
      people.forEach(function (person) {
        this.$._list.push('items', person);
      }.bind(this));
      ...
    }
    
  • 选项7:将此作为第二个参数传递:

在您的
forEach()
函数表达式中,
指的是
窗口
对象,而不是聚合物对象。(如果在此处使用,
将是
未定义的
,但您似乎不是这样。)

解决此问题的几个选项:

  • 选项1:使用,它不绑定自己的
    (假设您可以使用ES6):

  • 选项2:使用,无需回调(假设您可以使用ES6):

  • 选项3:用于一次推送所有阵列项(假设ES6对您可用):

  • 选项4:传入对聚合物对象的引用:

    onResponse: function (e) {
      var self = this;
      var people = e.detail.response.data;
      people.forEach(function (person) {
        self.$._list.push('items', person);
      });
      ...
    }
    
  • 选项5:传入对列表对象的引用:

    onResponse: function (e) {
      var list = this.$._list;
      var people = e.detail.response.data;
      people.forEach(function (person) {
        list.push('items', person);
      });
      ...
    }
    
  • 选项6:在函数表达式中显式绑定此:

    onResponse: function (e) {
      var people = e.detail.response.data;
      people.forEach(function (person) {
        this.$._list.push('items', person);
      }.bind(this));
      ...
    }
    
  • 选项7:将此作为第二个参数传递:


除了@tony19的答案,您还可以通过
bind

people.forEach(function (person) {
  this.$._list.push('items', person);
}.bind(this));

除了@tony19的答案之外,您还可以通过
bind

people.forEach(function (person) {
  this.$._list.push('items', person);
}.bind(this));

我认为这不是你所想的:)。试试这个问题的建议:我认为
这个
不是你所想的:)。尝试以下问题的建议:格栅!对于像我这样的初学者来说,这是一个很好的解释。多谢各位much@user2837615没问题,我认为使用
this
作为
forEach
的第二个参数应该包括
people.forEach(函数(人){this.$.\u list.push('items',person);},this)格栅!对于像我这样的初学者来说,这是一个很好的解释。多谢各位much@user2837615没问题,我认为使用
this
作为
forEach
的第二个参数应该包括
people.forEach(函数(人){this.$.\u list.push('items',person);},this)确实如此。增加了选项。的确如此。增加了选项。
people.forEach(function (person) {
  this.$._list.push('items', person);
}.bind(this));