我需要访问$options
vue 实例的属性,但getCurrentInstance().$options
尚未定义。
<script setup>
console.log(getCurrentInstance().$options) // undefined
</script>
我需要访问$options
vue 实例的属性,但getCurrentInstance().$options
尚未定义。
<script setup>
console.log(getCurrentInstance().$options) // undefined
</script>
在组合 API 中需要访问实例可能是 XY 问题。设置块旨在使用变量定义组件实例,这些变量可以在块中访问。
在其他情况下,
getCurrentInstance
函数可用于访问内部组件实例。它不属于 Vue 公共 API,但适用于像 HMR 这样的特殊情况。this
从options API对应来看getCurrentInstance().proxy
,应该是:或者,
script
可以将带有选项 API 的块一起使用script setup
而不依赖内部 API,只要它们不需要访问彼此范围的数据。