Vue在页面刷新后更新值

时间:2018-05-30 21:02:48

标签: vuejs2 vuejs-directive

<v-layout row wrap>
  <v-flex class="hidden-sm-and-down" md4>
    <v-subheader class="fields">Select Venue</v-subheader>
  </v-flex>
  <v-flex xs12 md8>
    <v-select class="select__box" :items="venues" v-model="venue" item-text="name" item-value="_id" label="Select Venue" prepend-icon="edit_location" autocomplete :error-messages="venueErrors" @blur="delayTouch($v.venue,200)" @input="delayTouch($v.venue,200)"></v-select>
  </v-flex>
</v-layout>


created: async function() {
  try {
    let response = await CommonRequest.getVenues();
    let array = response.data.venues;
    let i = 0;
    for (let ven of array) {
      this.venues[i] = Object.assign({}, this.venues, {
        name: ven.name,
        _id: ven._id
      });
      i++;
    }
  } catch (error) {
    if (error) console.log(error);
  }
}       

此处场地列表ID仅在我刷新页面后才更新。即使数据已到达(因为我执行了console.log(this.venues)以检查数据是否到达)

1 个答案:

答案 0 :(得分:0)

docs所述,数组元素的直接赋值不具有反应性。

  this.venues[i] = Object.assign({}, this.venues, {
    name: ven.name,
    _id: ven._id
  });

相反,你可以做

  this.$set(this.venues, i, Object.assign({}, this.venues[i], {
    name: ven.name,
    _id: ven._id
  }));

请注意,原始代码也缺少Object.assign

的第二个参数中的索引