setup的執行時組件對象還沒有創建,此時不能使用this來訪問data/computed/methods/props
我們可以通過 getCurrentInstance這個函數來返回當前組件的實例對象,也就是當前vue這個實例對象
<template> <div> </div> </template> <script lang="ts"> import {defineComponent, getCurrentInstance} from 'vue'; export default defineComponent({ setup () { const {proxy} = getCurrentInstance() console.log(proxy) } }) </script> <style scoped> </style>