如果我将变量设置为“计算”,Vue是否将变量添加到跟踪的依赖项中?

时间:2020-09-26 14:16:03

标签: javascript vue.js vue-composition-api

通常,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>

好像不是。我认为,如果这样做可能会造成无休止的更新循环。

0 个答案:

没有答案