// 假定有两个功能块 A 和 B,未封装前代码
<template>
<div ref="BRef"></div>
</template>
<script setup lang="ts">
// A 功能区代码
// B 功能区代码
const BRef = ref();
const doClick = () => {
//可以正常获取
console.log(BRef.value);
}
</script>
//封装抽离功能块
<template>
<div ref="BRef"></div>
</template>
<script setup lang="ts">
// A 功能区代码
import { A 相关 } from './A'
import { BRef, doClick } from './B'
//B 功能区 js 代码
const BRef = ref();
const doClick = () => {
//此处找不到,为 undefined
console.log(BRef.value);
}
export { BRef, doClick }
现在改造必须在最外层重新赋值一下,才能获取,有没有更优雅的写法
<template>
<div ref="BRefCopy"></div>
</template>
<script setup lang="ts">
// A 功能区代码
import { A 相关 } from './A'
import { BRef, doClick } from './B'
const BRefCopy = BRef;