Vue3 <script setup>语法糖使用 beforeRouteEnter
<script setup lang="ts">
import { ref, reactive, ComponentPublicInstance } from 'vue'
// * defineExpose暴露出来的方法,接口实现
interface IInstance extends ComponentPublicInstance {
getData(): void
}
defineOptions({
name: '***',
beforeRouteEnter(_to, _from, next) {
next((vm) => {
const instance = vm as IInstance
instance.getData() // 刷新列表数据(不缓存)
})
}
})
// 获取表格数据(示例方法)
const listData = ref([])
const getData = async () => {
listData.value = []
}
// * beforeRouteEnter中要用到的方法,需要暴露出来
defineExpose({ getData })
</script>