【发布时间】:2021-04-03 01:11:24
【问题描述】:
我有一个文件 vue 组件,其中一个属性 'todo-item' 具有这种结构
{
id:1,
text:'Buy tickets',
isDone: false,
person:'Boss',
location:'Boryspil',
childTask:null,
},
我已经计算了属性:
computed:{
hasLocation(){
return this.todoItem.location;
},
hasPerson(){
return this.todoItem.person;
},
hasChildTask(){
return this.todoItem.childTask;
},
hasParentTask(){
return true;
},
}
我想让所有对象属性都具有反应性,但是当我更改方法中的属性时:
deletePerson(){
this.$set(this.todoItem, 'person', null);
console.log(this.hasPerson);
console.log(this.todoItem.person);
},
todoItem.person 仍然没有反应,this.hasPerson 有旧值,this.todoItemPerson 显示空值。
我试图让它们在 created 方法上反应,但它仍然没有反应。
这是由组件整个js代码,没有模板和简称css:
<script>
import HoveredChip from "@/components/HoveredChip";
import {mapMutations} from 'vuex';
export default {
name: "TodoItem",
components: {HoveredChip},
props:['todo-item'],
data() {
return{
isActive : true,
}
},
computed:{
hasLocation(){
return this.todoItem.location;
},
hasPerson(){
return this.todoItem.person;
},
hasChildTask(){
return this.todoItem.childTask;
},
hasParentTask(){
return true;
},
people(){
return [
"dad",
"Ann",
"boss",
"prostitute"
]
},
places(){
return []
}
},
methods:{
...mapMutations(['addPersonMutation'],{
}),
moveToPerson(){
},
moveToLocation(){
},
moveToPatentTask(){
},
addLocation(){
},
addPerson(val){
this.addPersonMutation(val);
this.$set(this.todoItem,'person',val);
console.log(this.hasPerson);
console.log(this.todoItem.person);
},
addChildTask(){
},
deletePerson(){
this.$set(this.todoItem, 'person', null);
console.log(this.hasPerson);
console.log(this.todoItem.person);
},
deleteLocation(){
},
deleteChildTask(){
},
editLocation(){
},
savePerson(){
},
editChildTask(){
}
},
created(){
// this.$set(this.todoItem, 'person', 'undefined');
// this.$set(this.todoItem, 'location', '????');
// this.$set(this.todoItem, 'isDone', "....");
}
}
</script>
【问题讨论】:
-
你是从父母那里传递 todo-items 吗?
-
是和@kasvith 相同的问题,数据是
prop还是data? -
是的,我将 todo-item 作为父元素的属性传递,我认为这是主要问题,我必须在父元素中进行反应,或者在更新时替换父元素中的整个对象一些属性,无论如何它必须发送到服务器并在将来替换,所以我想我会尝试这种方式,如果添加新的 todo-item 我将使用数据对象。