【发布时间】:2020-01-16 10:42:14
【问题描述】:
我需要使用计算更改数据:
<template>
<div>{{ userDataTest }}</div>
</template>
props: {
exampleData: {
type: Object,
required: true,
},
},
computed: {
userDataTest: {
get: function() {
return this.exampleData;
},
set: function(newValue) {
console.log(newValue);
return newValue;
},
},
}
mounted () {
setTimeout(() => {
console.log('Change now to null!');
this.userDataTest = null;
}, 5000);
},
我使用 props 获取数据,接下来我使用 getter 和 setter 创建计算方法。我在<template> 中添加了userDataTest。我使用setter将this.userDataTest中的数据(使用mounted)更改为null。
在 setter 中的 console.log(newValue); 中,我看到 newValue 是 null,但在 <template> 中没有任何变化,我仍然有来自 getter 的数据。
为什么 setter 不将 <template> 中的数据更改为 null ?
【问题讨论】:
-
您的设置器除了返回输入值之外什么也没做。仅此一项不会有任何影响。
-
this.userDataTest总是指get方法返回的值,就是this.exampleData的值。 -
你不能改变一个道具