【问题标题】:How to reset a vue-infinite-loading element?如何重置 vue-infinite-loading 元素?
【发布时间】:2019-01-04 19:09:26
【问题描述】:

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

这是Code Pen注意:我无法让 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向exhibits.resultsexhibits.resultsMap添加10行

我尝试过的事情:

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

【问题讨论】:

  • 更新: 我找到了this post by the author,上面说你可以使用this.$refs.infiniteLoading.stateChanger.reset() 来重置。这将我引向this.$refs.infiniteLoading.status,但它是数字的,vue 文件中对状态的任何引用都像STATUS.loading。那么我怎么知道哪个状态意味着“完成”呢?
  • 更新: 结果 STATUS.COMPLETE 是 2。所以我这样做了:if(this.$refs.infiniteLoading.status == 2) { this.$refs.infiniteLoading.stateChanger.reset(); } 但它并没有像我想象的那样会做 - 它似乎没有做任何事情。它说没有更多的记录可以加载,即使有。

标签: vue.js vuejs2 vue-infinite-loading


【解决方案1】:

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

created(){
    if(this.$refs.InfiniteLoading){
        this.$refs.InfiniteLoading.stateChanger.reset(); 
    }
}

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多