Html Vuejs-在当前页面中的POST请求后获取对象ID
我想在同一个页面上实现保存和编辑。当然,我有很多字段输入,因此,我可以输入一些输入字段并Html Vuejs-在当前页面中的POST请求后获取对象ID,html,vue.js,vuetify.js,Html,Vue.js,Vuetify.js,我想在同一个页面上实现保存和编辑。当然,我有很多字段输入,因此,我可以输入一些输入字段并保存,而无需重新选择其他页面 我想要的是在POST请求后获得当前id,这样我就可以使用该id来补丁请求 Vuejs代码 我将感谢你的帮助。谢谢…假设您的API使用新ID响应POST请求,您只需将其设置为表单对象即可 axios.post(“api/v1/partner/”,this.form) 。然后(res=>{ console.log(res) this.isEdit=true this.form.id=
保存
,而无需重新选择其他页面
我想要的是在POST请求后获得当前id,这样我就可以使用该id
来补丁请求
Vuejs代码
我将感谢你的帮助。谢谢…假设您的API使用新ID响应
POST
请求,您只需将其设置为表单
对象即可
axios.post(“api/v1/partner/”,this.form)
。然后(res=>{
console.log(res)
this.isEdit=true
this.form.id=res.data.id//假设这是正确的属性名称
})
API对POST请求的响应是什么?它包括新的ID吗?@Phil,在POST request
之后,是的,我得到了新的ID。通常,人们使用PUT
来更新现有资源<代码>补丁就像是部分更新(例如,不是整个对象,只是选择属性)。@Phil,是的,我意识到了这一点。非常感谢您的解决方案,它比我预期的简单。:)谢谢你的否决票。既然这已经是公认的答案,你能留下一条评论来解释问题所在吗?这样我就可以为未来的读者修正它了?
<v-btn
color="primary"
v-if="isEdit === false"
small
:loading="loading"
@click="save"
>save</v-btn
>
<v-btn
color="primary"
small
:loading="loading"
@click="edit"
v-if="isEdit === true"
>edit</v-btn
>
<script>
export default {
data() {
return {
form: {},
isEdit: false
}
},
save() {
this.loading = true;
axios
.post(`api/v1/partner/`, this.form)
.then((res) => {
console.log(res);
this.isEdit = true;
})
.catch((err) => {
console.log(err.response);
this.loading = false;
this.snackbar.value = true;
this.$refs.form.validate(err.response.data);
});
},
edit() {
this.isEdit = true;
axios
.patch(`api/v1/partner/${this.form.id}/`, {
})
.then((res) => {
console.log(res);
// this.$router.push(`/partner/`);
this.loading = false;
})
.catch((err) => {
console.log(err.response);
this.loading = false;
});
},
}
</script>