在vue3使用$forceUpdate的方法

如果真的很需要在vue3中使用forceUpdate,同时又想使用 <script lang="ts" setup>的写法,那么可以这样写:

<template>
  <button @click="handleClick">按钮</button>
</template>
<style scoped lang="less"></style>
<script lang="ts" setup>
import { getCurrentInstance } from "vue";

const {
  proxy: { $forceUpdate },
}: any = getCurrentInstance();
function handleClick() {
  $forceUpdate();
  console.log($forceUpdate, "bingo!");
}
</script>
posted @ 2023-01-31 17:19  菌子乐水  阅读(8583)  评论(0编辑  收藏  举报