【发布时间】:2019-12-05 01:55:54
【问题描述】:
我正在使用 Vue.js 和引导模式创建一个小型 CRUD,但我遇到了更新用户的问题。
我使用 axios 处理任务,一旦我尝试 tupdate 用户,它就不能作为更新用户,而是作为创建用户。
我的模板文件
<button class="btn btn-primary" @click="modal">
<span>Create User</span>
</button>
<!-- Modal -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Body -->
<div class="modal-body">
<form>
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" v-model="input.name" v-validate.continues="'required'">
</div>
<div class="form-group">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" v-model="input.email" v-validate.continues="'required'">
</div>
<div class="form-group">
<label for="name">Phone Number</label>
<input type="text" name="phone_number" class="form-control" v-model="input.phone_number" v-validate.continues="'required'">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Close
</button>
<button v-if="add" type="button" class="btn btn-primary" @click.prevent="createUser">
Create
</button>
<button v-if="edit" type="button" class="btn btn-primary" @click.prevent="updateUser">
Update
</button>
</div>
</div>
</div>
</div>
我的脚本文件
update(id) {
this.add = false;
this.edit = true;
this.input = this.users[id];
$('#modal').modal('show');
},
updateUser() {
let $this = this;
axios.patch('/api/users/' + this.input.id, {
name: this.input.name,
email: this.input.email,
phone_number: this.input.phone_number,
}).then(function (response) {
console.log(response);
$this.fetchUser();
Swal.fire({
position: 'top-end',
type: 'success',
title: 'User has been updated',
showConfirmButton: false,
timer: 1500
})
$('#modal').modal('hide');
})
this.$validator.validateAll();
},
感谢您的所有帮助。
提前谢谢..
【问题讨论】:
-
有问题吗?
-
它不能用作更新,而是用作创建
-
你试过
put而不是patch吗? -
试过了,还是一样