在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
}

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐