关于Vue $forceUpdate的一些理解

在官方文档上仅仅有这一句话

迫使 Vue 实例重新渲染。注意它仅仅影响实例本身和插入插槽内容的子组件,而不是所有子组件。

我的理解是,所谓重新渲染,仅仅是指重新渲染DOM并与原有的DOM做code diff。对于有变更的,渲染到页面。结合官方解释,可以确认有两点不会发生:

  1. 不会重新触发生命周期钩子函数,比如mounted
  2. 不会渲染子组件的更新,即使子组件的props发生改变了。

既然如此,什么场景下会需要使用呢?

我们知道,在Vue中,响应数据发生改变后,会自动触发更新,但有一些条件,不会触发更新,比如数组的一些方法,或者添加data中并未提前定义的响应式数据。

举个例子,假设有一个列表需要渲染,我们在data中初始化了这个列表,并在mounted函数中通过fill去填充这个数组。fill这个api是不会触发自动更新的。

<template>
  <div class="hello">
    <div>
      <span v-for="(item, index) in arr" :key="index">{
          
   {
          
    item }}</span>
    </div>
    <hello-world ref="hello" :list="arr" />
  </div>
</template>

<script>
import HelloWorld from "@/components/HelloWorld.vue";
export default {
          
   
  name: "About",
  components: {
          
   
    HelloWorld,
  },
  data() {
          
   
    return {
          
   
      arr: Array(4),
    };
  },
  mounted() {
          
   
    this.arr.fill(0);
    setTimeout(() => {
          
   
      this.$forceUpdate();
    }, 3000);
    setTimeout(() => {
          
   
      this.$refs.hello.$forceUpdate();;
    }, 5000);
  },

在上面的示例中,页面会在3秒后才看到更新,子组件会在5秒后看到更新。这也就解释了forceUpdate的含义。

即强制更新因某些原因并未渲染到页面的,已经改变的,应该被渲染到页面的数据。

他所影响的仅仅是【触发render函数生成DOM -> 与原DOM 进行diff -> 更新视图】这个过程。

经验分享 程序员 微信小程序 职场和发展