javascript - 关于 Props 在 vuetify 表上发出的问题
问题描述
我有 2 个包含 app 和 /component/datatable.vue 的页面,但是当我单击方法 EditedItem 时出现问题。
避免直接改变 prop,因为只要父组件重新渲染,该值就会被覆盖。相反,使用基于道具值的数据或计算属性。正在变异的道具:“editedItem”
我该怎么做?
这是我的页面:
<template >
<v-container>
<v-col>
<Datatables
:headers="this.headers"
:items="items"
:editedItem="editedItem"
:defaultItem="defaultItem"
sort-by="calories"
class="elevation-1"
/>
</v-col>
<v-spacer></v-spacer>
<br />
<v-col></v-col>
</v-container>
</template>
<script>
import Datatables from "../../components/Datatable/CrudDatatable";
export default {
components: {
Datatables
},
data() {
return {
headers: [
{
text: "Dessert (100g serving)",
align: "start",
sortable: false,
value: "name"
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" },
{ text: "Carbs (g)", value: "carbs" },
{ text: "Protein (g)", value: "protein" },
{ text: "Actions", value: "action", sortable: false }
],
items: [],
editedIndex: -1,
editedItem: {
name: "",
calories: 0,
fat: 0,
carbs: 0,
protein: 0
},
defaultItem: {
name: "",
calories: 0,
fat: 0,
carbs: 0,
protein: 0
}
};
},
created() {
this.initialize();
},
methods: {
initialize() {
this.items = [
{
name: "Frozen Yogurt",
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0
},
{
name: "Ice cream sandwich",
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3
}
];
}
}
};
</script>
<style>
</style>
这是我的组件
<template>
<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>My CRUD</v-toolbar-title>
<v-divider class="mx-4" inset vertical></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="500px">
<template v-slot:activator="{ on }">
<v-btn color="primary" dark class="mb-2" v-on="on">New Item</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-row>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.name" label="Dessert name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.calories" label="Calories"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.fat" label="Fat (g)"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.carbs" label="Carbs (g)"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.protein" label="Protein (g)"></v-text-field>
</v-col>
</v-row>
</v-container>
</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.action="{ 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>
</template>
<script>
export default {
props: {
headers: {
type: Array
},
items: {
type: Array
},
editedItem: {
type: Object
},
defaultItem: {
type: Object
},
editedIndex:{
type: Number
}
},
data: () => ({
dialog: false,
}),
computed: {
formTitle() {
return this.editedIndex === -1 ? "New Item" : "Edit Item";
}
},
watch: {
dialog(val) {
val || this.close();
}
},
methods: {
editItem(item) {
this.editedIndex = this.items.indexOf(item);
this.editedItem = Object.assign(this.items[this.editedIndex], this.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;
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem);
this.editedIndex = -1;
}, 300);
},
save() {
if (this.editedIndex > -1) {
Object.assign(this.items[this.editedIndex], this.editedItem);
} else {
this.items.push(this.editedItem);
}
this.close();
}
}
};
</script>
解决方案
推荐阅读
- neo4j - 在 graphql/neo4j 中,关系 IN 和 OUT 与 cypher 的关系没有按预期工作
- r - 在 Plotly 中使用 for 循环绘制箱线图
- html - 如何仅使用 SCSS 将 display:block 更改为 display:none?
- lua - 通过引用与 ipairs 循环访问表的性能
- powershell - 如果 move-vm cmdlet 中发生终止错误,输出是什么
- r - 使用特定值从相关矩阵中提取行名和列名
- rxjs - Angular 11反应形式订阅输入订阅的值变化无限
- javascript - 如何使用 JavaScript 或 jQuery 仅在移动视图上重新排序 li 元素?
- c# - 如何使用不同的参数多次设置相同的方法?
- winapi - 使用 Windows API 设置文件的权限