vue.js - Vue 3 中的 this.$forceUpdate 等效项 - 组合 API?
问题描述
this.$forceUpdate()
在 Vue 2 中,可以使用实例方法手动更新组件。我们如何在 Vue 3 - Composition API(内部设置方法)中强制更新组件?
setup(props, context) {
const doSomething = () => {
/* how to call $forceUpdate here? */
}
return {
doSomething
}
}
提前致谢。
解决方案
$forceUpdate
is still available in Vue3, but you won't have access to it in the setup()
function. If you absolutely need to use it, you can use object API component or this fancy trick...
app.component("my-component", {
template: `...`,
methods: {
forceUpdate(){
this.$forceUpdate()
}
},
setup(props) {
const instance = Vue.getCurrentInstance();
Vue.onBeforeMount(()=>{
// instance.ctx is not populated immediately
instance.ctx.forceUpdate();
})
return {doSomething};
},
})
If this seems like a ridiculous solution, trust your Judgement. Ideally your application would not rely on forceUpdate
. If you are relying on it, it likely means that something is miss-configured, and that should be the first thing to resolve.
推荐阅读
- swift - 在表格视图中显示谷歌地图搜索列表
- android - 在 Android 中使用 Shared ViewModel 在不同 ViewModel 之间共享数据
- python - 如何在 Fluter 中使用 Flask Api?
- spring-kafka - 在 Spring-Kafka 中,我们是否有任何监听器或拦截器在每次重试尝试时被调用?
- python - 如何在运行时检查 TypeVar 的类型
- ios - 名为“textFieldShouldReturn(textField:)”的方法的返回值有什么区别?
- javascript - 如何异步运行函数?
- javascript - 如何获取从 API 中提取的数据中的值的键?
- javascript - 如何使用 API 使谷歌地址自动填充,用数字和街道替换完整地址?
- python - Python Unittest 用参数模拟函数