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