Skip to content

强制组件更新

  1. 刷新整个页面(最 low 的,可以借助 route 机制,不推荐)

  2. 使用 v-if 标记(比较 low 的,有时候不生效,不推荐)

    vue
    <script setup>
    import comp from '@/views/comp.vue'
    const refresh = ref(true)
    
    const refreshComp = () => {
      refresh.value = false
      nextTick(() => {
        refresh.value = true
      })
    }
    </script>
    
    <template>
      <comp v-if="refresh"></comp>
      <button @click="refreshComp()">刷新comp组件</button>
    </template>
  3. 使用内置的 forceUpdate 方法(较好的)

  4. 使用 key-changing 优化组件(最好的)

    vue
    <template>
      <comp :key="refresh"></comp>
    </template>

基于 MIT 许可发布