1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > vue3获取当前组件实例的方法getCurrentInstance

vue3获取当前组件实例的方法getCurrentInstance

时间:2021-03-03 21:39:05

相关推荐

vue3获取当前组件实例的方法getCurrentInstance

vue3获取当前组件实例的方法getCurrentInstance

在vue3中,setup 是围绕 beforeCreate 和 created 生命周期钩子运行的,setup的执行时组件对象还没有创建,此时不能使用this来访问data/computed/methods/props,我们可以通过 getCurrentInstance这个函数来返回当前组件的实例对象,也就是当前vue这个实例对象

import {getCurrentInstance, onMounted, ref } from 'vue'export default ({components:{Header},setup(props, context) {const {ctx } = getCurrentInstance()onMounted(()=>{ctx.$http.get('/user')})return {}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。