前端

Vue3 中如何使用this

caught (in promise) TypeError: Cannot read properties of null (reading 'parentNode')
 <script setup>
    import {getCurrentInstance} from '@vue/runtime-core'
 
 
    const currentInstance = ref()
 
 
    onMounted(() => {
 
        /**
         * 此处这样使用时因为Vue3不同于Vue2,在 Vue3的setup中我们是无法访问到this的,所以我们需要借助一个方法,
         * 那就是getCurrentInstance,该方法返回了当前的实例对象
         *
         * 注意!!!!!
         * 不要把该函数当作是optionsApi中来获取 this 使用。
         * 该方法只在 setup、生命周期函数中有效,在方法中是无效的
         * */
        currentInstance.value = getCurrentInstance()
    });
</script>