通常,Vue会在computed
中获取某个值的值变化:
const App = {
setup() {
const message = Vue.ref("hello world");
const computed = Vue.computed(() => {
return message.value + "!!!";
});
return {
message,
computed
}
}
};
Vue.createApp(App).mount('#root')
<script src="https://unpkg.com/vue@next"></script>
<div id="root">
<input v-model="message"/>
computed: {{ computed }}
</div>
但是,如果我没有得到一个值,而是设置了该值怎么办?是否也使它成为computed
的跟踪依赖项?
const App = {
setup() {
const message = Vue.ref("hello world");
const computed = Vue.computed(() => {
message.value = "hello world!!!"
return "hello world!!!";
});
return {
message,
computed
}
}
};
Vue.createApp(App).mount('#root')
<script src="https://unpkg.com/vue@next"></script>
<div id="root">
<input v-model="message"/>
computed: {{ computed }}
</div>
好像不是。我认为,如果这样做可能会造成无休止的更新循环。