代码之家  ›  专栏  ›  技术社区  ›  Success Man

如何按页码或上一次单击vue显示内容?

  •  0
  • Success Man  · 技术社区  · 7 年前

    我的vue组件如下所示:

    <template>
        ...
            <b-col v-for="item in items"
                   v-bind:key="item.id"
                   cols="2">
                ...
                <strong slot="header" class="text-dark" :title="item.tittle" >{{item.tittle}}</strong><br/>
                ...
                <strong class="bg-warning text-dark"><i class="fa fa-money"></i> <b>{{item.price}}</b></strong><br/>
                ...
            </b-col>
        ...
            <b-pagination size="sm" :total-rows="itemsPagination.total" :per-page="itemsPagination.per_page" v-model="itemsPagination.current_page" prev-text="Prev" next-text="Next" hide-goto-end-buttons/>
        ...
    </template>
    <script>
        export default {
            ...
            data () {
              return{
                  items: '',
                  itemsPagination: ''
              }
            },
            mounted: function () {
                this.getItems()
            },
            methods: {
                getItems() {
                    let params = []
                    ...
                    let request = new Request(ApiUrls.items + '?' + params.join('&'), {
                        method: 'GET',
                        headers: new Headers({
                            'Authorization': 'bearer ' + this.$session.get(SessionKeys.ApiTokens),
                            'Content-Type': 'text/plain'
                        })
                    })
                    fetch(request).then(r=> r.json())
                        .then(r=> {
                            this.items = r.data
                            this.itemsPagination = r
                        })
                        .catch(e=>console.log(e))
                }
            }
        }
    </script>
    

    如果我 console.log(this.itemsPagination)

    enter image description here

    我在应用程序中对分页的看法如下:

    enter image description here

    如果执行脚本,它将在第1页显示项目的内容。但如果单击第2页等,项目的内容不会更改。我仍然很困惑如何让它正常工作

    更新

    我正在使用coreui

    https://coreui.io/vue/demo/#/base/paginations

    https://github.com/coreui/coreui-free-vue-admin-template/blob/master/src/views/base/Paginations.vue

    https://bootstrap-vue.js.org/docs/components/pagination

    3 回复  |  直到 7 年前
        1
  •  1
  •   Roy J    7 年前

    你是想 v-model="itemsPagination.current_page" ,但您需要初始化 itemsPagination

        data () {
          return{
              items: '',
              itemsPagination: ''
          }
        }
    

    Vue cannot detect property addition or deletion 项目空缺 current_page :

        data () {
          return{
              items: '',
              itemsPagination: {
                current_page: 1
              }
          }
        }
    

    更新: example here . 双击右上角进行编辑,然后粘贴到以下代码中:

    <template>
      <div>
        <h6>Default</h6>
        <b-pagination size="md" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
        </b-pagination>
        <br>
    
        <h6>Small</h6>
        <b-pagination size="sm" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
        </b-pagination>
        <br>
    
        <h6>Large</h6>
        <b-pagination size="lg" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
        </b-pagination>
        <br>
    
        <div>currentPage: {{itemsPagination.current_page}}</div>
      </div>    
    </template>
    
    <script>
    export default {
      data () {
        return {
          itemsPagination: {
            current_page: 1
          }
        }
      }
    }
    </script>
    
    <!-- pagination-1.vue -->
    
        2
  •  0
  •   SiarheiK    7 年前

    我相信,你不会更新 items

    你能试一下吗

    data () {
              return{
                  container:{ 
                  // for Vue.set I didnt found how to do it without nested fields for data object, so did like that on my own project
                      items: '',
                      itemsPagination: ''
                  }
              }
            },
    

    Vue.set(this.container, 'items', r.data); 
    // or this.$set, if you dont use arrow function here, and not Vue accessable
    

    看见 https://vuejs.org/v2/guide/reactivity.html

    如果没有帮助,请检查 console.log(this) 在你的内心 fetch

        3
  •  0
  •   Jorge Urosa    7 年前

    也许这不是回答你的最佳方式(重定向到视频),但我认为这段视频将比我更清楚地了解正在发生的事情以及如何解决它。

    https://youtu.be/7lpemgMhi0k?t=17m42s

    推荐文章