1.引入 getCurrentInstance
<template>
<div>
this.指向
<button @click="getData">获取data上的变量</button>
</div>
</template>
<script>
import { getCurrentInstance } from 'vue';
export default {
name: 'ZzzThis',
setup() {
// console.log(this);
let {ctx} = getCurrentInstance()
let _this = ctx
console.log(ctx);
function getData(){
console.log(_this.d);
}
return{
getData
}
},
data(){
return {
d:"拿到了"
}
}
};
</script>
<style>
</style>