【发布时间】:2022-10-21 07:52:28
【问题描述】:
更新数据后如何不刷新或重新加载页面? 我正在使用 Modal 编辑数据,但问题是页面在保存后仍然刷新,是否有其他方法可以解决此问题?
<button class="btn btn-warning" @click="edit(item)"><i class="fa fa-edit"></i></button>
模态:
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Employee Edit</h5>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" v-model="formEdit.name">
</div>
......
脚本:(edit用于显示数据,update用于更新数据)
edit(item){
const vm = this;
vm.formEdit.name = item.name;
vm.formEdit.address = item.address;
vm.formEdit.position = item.position;
this.selectedId = item.id;
$('#editModal').modal('show');
},
update(){
const vm = this;
axios.put(`/employees/${vm.selectedId}`, this.formEdit)
.then(function (response){
alert('Employee Updated')
location.reload();
})
.catch(function (error){
console.log(error);
});
}
这适用于 Laravel 8 和 Vue
员工组成:
props: ['employee'],
data() {
return {
employeeList: this.employee,
form:{
name: null,
address: null,
position: null
},
formEdit:{
name: null,
address: null,
position: null
},
selectedId: null
}
}
【问题讨论】:
-
嗯,事实上,这就是响应式框架的主要目的。成功执行 put 请求后,您应该将
response.data传播到您的主要组件。请分享您要显示员工数据的组件。 -
@Luciano你好,请看我上面更新的代码。谢谢你,先生
-
我不确定是否理解。更新员工后,您想在哪里显示更新的数据?你有桌子吗?显示组件?
-
@Luciano 是的,我有表格,这就是我推它的地方
vm.employeeList.push(response.data.data).. 你可以说employeeList是显示我的数据的表格列表
标签: laravel vue.js vuejs2 laravel-8