4

我有一个展品清单。用户可以添加展品。当用户添加展品时,vue-infinite-loading不会注意到展品数量的变化,因此即使有更多要展示,它也会显示“列表结束”。

这是一支代码笔注意:我无法让 vue-infinite-loading 在沙箱中工作,但它肯定在我的网站上工作。

这是代码:

HTML:

<div id="app">
  <button @click="addTenRows()">Add 10 Rows</button>
  <table cellpadding="10">
    <tr><th>id</th><th>Name</th><th>Description</th></tr>
    <tr v-for="exhibit in exhibits.resultsMap">
      <td>{{exhibit.id}}</td>
      <td>{{exhibit.name}}</td>
      <td>{{exhibit.desc}}</td>
    </tr>
  </table>
  <infinite-loading 
      :distance="10" 
      @infinite="loadMore" 
      ref="infiniteLoading" 
      identifier="exhibitLoader">
      <span slot="no-more">
        <h5>
          <i class="fa fa-check-circle"></i> End of Results
          <span v-if="exhibits.results">
              : <strong>{{exhibits.meta.totalRecords}} shown</strong>
          </span>
        </h5>
      </span>
  </infinite-loading>
</div>

JS:

new Vue({
  el: '#app',
  methods: {
    addTenRows(){
      this.exhibits.meta.totalRecords += 5;
    },
    loadMore(){
      this.exhibits.results.push('16');
      this.exhibits.results.push('17');
      this.exhibits.results.push('18');
      this.exhibits.results.push('19');
      this.exhibits.results.push('20');

      this.exhibits.resultsMap[ '16' ] = { 'id':'16', 'name': 'STR-16', desc: 'Exhibit number 16' };
      this.exhibits.resultsMap[ '17' ] = { 'id':'17', 'name': 'STR-17', desc: 'Exhibit number 17' };
      this.exhibits.resultsMap[ '18' ] = { 'id':'18', 'name': 'STR-18', desc: 'Exhibit number 18' };
      this.exhibits.resultsMap[ '19' ] = { 'id':'19', 'name': 'STR-19', desc: 'Exhibit number 19' };
      this.exhibits.resultsMap[ '20' ] = { 'id':'20', 'name': 'STR-20', desc: 'Exhibit number 20' };
    }
  },
  data: {
    exhibits: {
      meta: {
        totalRecords: 25
      },
      results: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'],
      resultsMap:{
        '1':{ id: 1, name: 'STR-01', desc: 'some exhibit' },
        '2':{ id: 2, name: 'THR-20', desc: 'another exhibit' },
        '3':{ id: 3, name: 'STR-02', desc: 'a third' },
        '4':{ id: 4, name: 'THR-21', desc: 'This is the fourth Exhibit' },
        '5':{ id: 5, name: 'STR-03', desc: 'Number 5' },
        '6':{ id: 6, name: 'THR-22', desc: 'Up Up Down Down' },
        '7':{ id: 7, name: 'STR-04', desc: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis ultrices ' },
        '8':{ id: 8, name: 'THR-23', desc: 'Praesent augue tellus, aliquet sit amet tellus in' },
        '9':{ id: 9, name: 'STR-05', desc: 'Cras eget augue tellus.' },
        '10':{ id: 10, name: 'THR-24', desc: 'Nunc facilisis elit at efficitur ullamcorper.' },
        '11':{ id: 11, name: 'STR-06', desc: 'In vel turpis ex. Curabitur mollis ac sapien eget aliquet' },
        '12':{ id: 12, name: 'THR-25', desc: 'Morbi eget lacus eu mi tincidunt scelerisque ac sed metus' },
        '13':{ id: 13, name: 'STR-07', desc: 'Donec consequat est maximus, venenatis velit a, vehicula felis' },
        '14':{ id: 14, name: 'THR-26', desc: 'Mauris orci neque, ullamcorper non blandit non' },
        '15':{ id: 15, name: 'STR-08', desc: 'Vivamus efficitur erat vel consectetur sagittis' }
      }
    }
  }
});

在上面的示例中,发生了什么:

  1. 页面加载,显示 15 条记录
  2. 用户单击“添加 10 条记录”按钮
  3. exhibits.meta.totalRecords增加 10
    • 应该告诉无限加载器刷新
  4. 用户向下滚动到底部 10px 以内,无限加载显示一个微调器,而 loadMore 将 10 行添加到exhibits.resultsexhibits.resultsMap

我尝试过的事情:

  • 强制重置:
    • addTenRows在我加十之后meta.totalRecords
    • this.$refs.infiniteLoading.stateChanger.reset();- 没有什么变化
    • this.$refs.exhibitLoader.stateChanger.reset();- 错误,exhibitLoader未定义。
    • this.exhibitLoader.stateChanger.reset();- 错误,exhibitLoader未定义。
4

1 回答 1

16

好吧,您已经添加了 ref="infiniteLoading",因此您需要验证组件是否已创建:

created(){
    if(this.$refs.InfiniteLoading){
        this.$refs.InfiniteLoading.stateChanger.reset(); 
    }
}
于 2019-03-05T23:18:04.863 回答