我是Vue.js和JavaScript的新手。我只想知道如何更新数组(在“添加/编辑/删除”上),而不是再次重新加载所有数据。仅在打开带有“地址”的页面时加载所有数据。
这是我的代码:
<template v-else>
<div>
<div class="page-header with-actions">
<h1>Addresses</h1>
<div class="actions">
<b-btn @click="addAddress" variant="primary"><i class="oi oi-plus"></i> Add Address</b-btn>
</div>
</div>
<div v-if="isLoaded === true">
<div v-if="addressList.length">
<b-form @submit.prevent="submit">
<validation-summary :errors="errors"></validation-summary>
<div class="row">
<div class="col-sm-4" v-for="address in addressList">
<div class="card bg-light mb-4">
<div class="card-header">
{{address.countryName}}
<div class="float-right">
<b-btn @click="editAddress(address.id)" class="btn btn-default btn-sm" data-toggle="tooltip" title="Edit address">
<i class="fa fa-edit" aria-hidden="true"></i>
</b-btn>
<b-btn @click="deleteAddress(address.id)" class="btn btn-default btn-sm" data-toggle="tooltip" title="Delete address">
<i class="fa fa-trash" aria-hidden="true"></i>
</b-btn>
</div>
</div>
<div class="card-body">
<em class="card-text">{{address.city}},</em><br />
<em class="card-text">{{address.streetAddress}},</em><br />
<em class="card-text">{{address.zip}}</em><br />
</div>
</div>
</div>
</div>
</b-form>
</div>
<div v-else class="alert alert-info" role="alert">
There are no <strong>addresses</strong> yet!
</div>
</div>
<add-address-modal ref="addAddressModal" @success="addressAdded"></add-address-modal>
<edit-address-modal ref="editAddressModal" @success="addressEdited></edit-address-modal>
</div>
</template>
<script>
import Vue from 'vue';
import modalFormMixin from 'mixins/modal-form-mixin';
import formMixin from 'mixins/form-mixin';
import accountService from 'services/account-service';
import AddAddressModal from './add-address-modal';
import EditAddressModal from './edit-address-modal';
export default {
mixins: [modalFormMixin],
mixins: [formMixin],
components: {
'add-address-modal': AddAddressModal,
'edit-address-modal': EditAddressModal
},
data() {
return {
addressList: [],
isLoaded: false,
}
},
async beforeRouteEnter(to, from, next) {
next(async (c) => await c.initData());
},
methods: {
async initData() {
await this.loadAddressList(); //when opening page
this.isLoaded = true;
},
async loadAddressList() {
this.addressList = await accountService.getAddressList();
},
addAddress(data) {
this.$refs.addAddressModal.show();
},
editAddress(id) {
this.$refs.editAddressModal.show(id);
},
async deleteAddress(id) {
await accountService.deleteAddress(id);
this.loadAddressList(); //need to change
this.$root.successToast('Address deleted');
},
addressAdded() {
this.loadAddressList(); //need to change
this.$root.successToast('Address added');
},
addressEdited() {
this.loadAddressList(); //need to change
this.$root.successToast('Address edited');
}
}
}
</script>
所以我需要更改loadAddressList(),因为它在Add / Edit / Detele之后再次重新加载了所有地址数据。最好的解决方案是什么?
答案 0 :(得分:2)
我以前在这个问题上犯规。与Reactivity有关,以及更改可能会延迟一下的时间。
解决问题的示例。
methods: {
async loadAddressList() {
this.addressList = await accountService.getAddressList();
await this.$nextTick() // waits for the next event tick before completeing function.
},
}
另外,请查看Array Change Detection。替换数组可以有效地进行操作,以重用DOM元素。
好运。
答案 1 :(得分:0)
我想知道您是否可以使用then
?因此,例如,在您的initiData
方法中,您可以这样做吗?
async initData() {
this.loadAddressList()
.then(() => {
this.isLoaded = true;
});
},
这意味着您不需要更新loadAddressList方法并使其等待或执行它实际上不应该负责的其他功能吗?
更新:您还应确保loadAddressList
方法返回一些值,因为它使用async
关键字返回诺言。
async loadAddressList() {
this.addressList = await accountService.getAddressList();
return true
},
然后.then()
将在您的initData()
方法中起作用。
答案 2 :(得分:0)
使用vue 2.0时,最好的选择是确保每次数据更改都会触发dom更新,并带有关联数组(对象对象)的Vue.set,但是为了更有效地执行更新/创建请求,应返回对象,那么您可以将其压入数组或使用此方法:
import Vue from 'vue'
createAddress(data) {
axios.POST('your-url', data).then( address => {
Vue.set($store.addresses, address.id, address)
})
}
要使用它,您必须像这样更改关联的地址数组:
import Vue from 'vue'
getAddresses(state) {
axios.GET('your-url').then(addresses => {
addresses.forEach(address => {
Vue.set(state.addresses, address.id, address)
})
})
}
您也可以使用
Object.assign(state.addresses[address.id], address)
但是,根据我的经验,有时由于未知原因有时不会更新dom。
那么您的address_list必须经过计算,指向状态管理中的地址(vuex或其他)
computed: {
address_list() {
return $store.addresses
}
}