vue3 封装js中使用getCurrentInstance
封装 axios的时候,需要 用弹窗,这个弹窜调用必须用 getCurrentInstance
请问这个 怎么做呢。。
vue3 封装js中使用getCurrentInstance
封装 axios的时候,需要 用弹窗,这个弹窜调用必须用 getCurrentInstance
请问这个 怎么做呢。。
在Vue2的任何一个组件中想要获取当前组件的实例可以通过 this 来得到,而在Vue3中我们大量的代码都在 setup 函数中运行,并且在该函数中 this 指向的是 undefined,这时就需要使用getCurrentInstance
<template>
<div>
<p>{{ num }}</p>
</div>
</template>
<script>
import {ref, getCurrentInstance} from 'vue'
export default {
setup() {
const num = ref(3)
const instance = getCurrentInstance()
console.log(instance)
return {num}
}
}
</script>
可以看到 ctx 和 proxy 的内容十分类似,只是后者相对于前者外部包装了一层 proxy,由此可说明 proxy 是响应式的