业务中有需要使用beforeRouterEnter又不想写在router.js中,于是使用了defineComponent与setup方式混写
要点是如果要引用setup中的方法,需要用defineExpose
暴露出去
<script>
import { defineComponent } from "vue";
export default defineComponent({
name: "CommonRefresh",
beforeRouteEnter(to, from, next) {
next((vm) => {
vm.backFrom(from)
});
},
});
</script>
<script setup>
import { useRouter } from "vue-router";
const router = useRouter();
const backFrom = (from) => {
router.replace({
path: from?.fullPath,
});
};
defineExpose({
backFrom,
});
</script>