gpt4 book ai didi

javascript - 在 Vue js 中使用 axios 会丢失对象的属性

转载 作者:行者123 更新时间:2023-12-03 06:47:32 25 4
gpt4 key购买 nike

我正在使用 axios,我不明白为什么对象的某些属性会丢失。请看这段代码

<template>
<div>
<v-message-box :data="mBox" />
<v-data-table :headers="headers" :items="items" sort-by="calories" class="elevation-1">
<template v-slot:top>
<v-toolbar flat color="white">
<v-toolbar-title>Coplaint list</v-toolbar-title>
<v-divider class="mx-4" inset vertical></v-divider>
<v-spacer></v-spacer>
<v-btn color="primary" @click="btnNewItem" dark class="mb-2">New Item</v-btn>
<v-dialog v-model="dialog" max-width="500px">
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>

<v-card-text>
<v-complaint :complaint="editedItem" />
</v-card-text>

<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="close">Cancel</v-btn>
<v-btn color="blue darken-1" text @click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.actions="{ item }">
<v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
<v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" @click="initialize">Reset</v-btn>
</template>
</v-data-table>
</div>
</template>

<script>
import vComplaint from "../create/v-complaint";
import Api from "@/api/Api";
import vMessageBox from "../commons/v-message-box";
import MessageBox from "../commons/messagebox.js";
import { mapGetters, mapActions } from "vuex";

export default {
name: "v-complaint-table",
data: () => ({
dialog: false,
headers: [],
items: [],
editedIndex: -1,
editedItem: {},
defaultItem: null,
mBox: new MessageBox(),
}),
props: ["patient"],

methods: {

// Some functions ....
editItem(item) {
this.editedIndex = this.items.indexOf(item);
this.editedItem = this.toObject(item);
this.dialog = true;
},

deleteItem(item) {
const index = this.items.indexOf(item);
confirm("Are you sure you want to delete this item?") &&
this.items.splice(index, 1);
},

close() {
this.dialog = false;
this.$nextTick(() => {
this.editedItem = Object.assign({}, this.defaultItem);
this.editedIndex = -1;
});
},

save() {
let mBox = this.mBox
if (this.editedIndex > -1) {
let complaint = this.editedItem;
Api()
.put("/complaint_request", {
complaint
})
.then(() => {
console.log("Updated");
})
.catch(e => {
mBox.showMessage("Error", e, "error");
console.log(e);
});
Object.assign(
this.items[this.editedIndex],
this.toTemplate(this.editedItem)
);
} else {
var complaint = this.editedItem;
var _this = this
console.log(this.editedItem)
Api()
.post("/complaint_request", {
complaint
})
.then(respone => {
_this.editedItem.id = respone.data;
console.log(_this.editedItem)
_this.items.push(this.toTemplate(_this.editedItem));
})
.catch(e => {
mBox.showMessage("Error", e, "error");
console.log(e);
});
}
this.close();
},
};
</script>
在第一次打印中,我采用 this.editedItem 的属性。
the result of the first print
在第二次打印中,我采用 this.editedItem 的属性。
the result of the second print
我不明白为什么字符和日期属性在第二次打印中丢失了。我知道这两个属性都是嵌套的。我认为问题与 axios 及其异步有关。
api.js
import axios from 'axios'

export default() => {
return axios.create({
baseURL: 'http://127.0.0.1:8000/api',
//baseURL: 'http://192.168.137.1:8080/api',
withCredentials: false,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'common': '',
//'token': ''
}
})
}

最佳答案

close() , 你分配

this.editedItem = Object.assign({}, this.defaultItem);
由于您调用 this.closesave 中的 promise 之前解决,然后 this.editedItem重置为 this.defaultItem ,我认为缺少日期。
所以最好调用 close在 promise 中 .then打回来。
编辑:修复名称

关于javascript - 在 Vue js 中使用 axios 会丢失对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com