Vue3中如何使用this
在v3中,使用setup()来代替了以前data、methods函数等。因为,setup执行是在created之前。所以,没有this。但是又想使用$parent,$refs等方法import { getCurrentInstance, ComponentInternalInstance } from 'vue'setup() {// as ComponetInternalInstance表示类型
·
在v3中,使用setup()来代替了以前data、methods函数等。因为,setup执行是在created之前。所以,没有this。但是又想使用$parent,$refs等方法
import { getCurrentInstance, ComponentInternalInstance } from 'vue'
setup() {
// as ComponetInternalInstance表示类型断言,ts时使用。否则报错,proxy为null
const { proxy } = getCurrentInstance() as ComponetInternalInstance
proxy.$parent
proxy.$refs
proxy.$nextTick
proxy.$attrs
proxy.$data
proxy.$el
proxy.$emit
proxy.$forceUpdate
proxy.$options
proxy.$props
proxy.$root
proxy.$slots
proxy.$watch
}
更多推荐
已为社区贡献1条内容
所有评论(0)