简体   繁体   English

Vue.js 推送不更新视图

[英]Vue.js Push doesn't update view

I create a array which contains objects describing information of buttons.我创建了一个数组,其中包含描述按钮信息的对象。 In my template I have a class active which is represent the active button.在我的模板中,我有一个 class active ,它代表活动按钮。 By default the first button of the array is active.默认情况下,数组的第一个按钮是活动的。

template:模板:

<b-button-group class="page-buttons mt-2" size="sm">
   <b-button v-for="btn in buttons" 
      :key="btn.key" 
      @click="selectPage(btn.key)"
      v-bind:class="{'active': btn.active}">{{ btn.caption }}
   </b-button>
 </b-button-group>

Script:脚本:

methods: {
    $createPaginationButtons: function(numberParameters) {
      const numberPages = Math.ceil(numberParameters / NUMBER_ELEMENT_PER_PAGE);
      this.buttons = [];

      for (let i = 0; i < numberPages; i++) {
        this.buttons.push({
          caption: `PAGE ${i + 1}`,
          active: (i === 0),
          key: (i + 1)
        });
      }

      Vue.set(this.buttons[0], 'active', true);
   }
}

The buttons array is initialize in data as an empty array.按钮数组在数据中初始化为空数组。 when my buttons array changes, the view is updated and display the correct number of buttons but my class active is not triggered.当我的按钮数组发生变化时,视图会更新并显示正确数量的按钮,但不会触发我的 class active

Have you any idea.你有什么想法吗?

Push is a wrapped method so it will trigger a view update. Push 是一种包装方法,因此它会触发视图更新。

Do you have declared buttons on the data property?您是否在数据属性上声明了按钮?

data() {
     buttons:[]
}

Since you are creating the active property as i === 0 you don't need Vue.set.由于您正在创建 active 属性为 i === 0,因此您不需要 Vue.set。

I don't know if has something to do but instead of making buttons empty and then pushing, use an aux variable to create the array and then just do this.buttons = auxVariable.我不知道是否有什么可做的,但不是让按钮为空然后按下,而是使用 aux 变量创建数组然后只做 this.buttons = auxVariable。 This should trigger an update.这应该触发更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM