Vue 组合式 API 强制刷新页面:使用 $forceUpdate() 方法
在 Vue 组合式 API 中,可以使用 Vue 的内置方法 $forceUpdate() 来强制刷新页面。在需要刷新的地方调用该方法即可。
示例代码:
import { reactive, onMounted } from 'vue';
export default {
setup() {
const state = reactive({
data: [],
});
const refreshPage = () => {
state.data = [1, 2, 3]; // 更新数据
// 调用 $forceUpdate() 方法强制刷新页面
this.$forceUpdate();
}
onMounted(() => {
refreshPage(); // 页面加载时调用刷新方法
});
return {
state,
refreshPage,
}
}
}
在上述代码中,我们在 refreshPage() 方法中更新了数据,并通过 this.$forceUpdate() 方法强制刷新页面。在页面加载时,我们调用了 refreshPage() 方法,以便在页面加载时自动刷新。
原文地址: https://www.cveoy.top/t/topic/mBu5 著作权归作者所有。请勿转载和采集!