You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
947 B
46 lines
947 B
7 months ago
|
import { useRouter } from 'vue-router';
|
||
|
import { onBeforeUnmount, onDeactivated, onActivated } from 'vue';
|
||
|
|
||
|
export function useNavigate() {
|
||
|
let isBack = false;
|
||
|
let isAlive = true;
|
||
|
const router = useRouter();
|
||
|
console.log(router);
|
||
|
function navigateBack() {
|
||
|
if (isAlive && !isBack) {
|
||
|
isBack = !isBack;
|
||
|
router.go(-1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function navigateBackV2() {
|
||
|
if (isAlive && !isBack) {
|
||
|
isBack = !isBack;
|
||
|
const match = router.currentRoute.value.matched;
|
||
|
if (match.length > 1) {
|
||
|
const backRoute = match[match.length - 2];
|
||
|
router.push({ name: backRoute.name });
|
||
|
} else {
|
||
|
router.go(-1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
onBeforeUnmount(() => {
|
||
|
isAlive = false;
|
||
|
});
|
||
|
// onActivated(() => {
|
||
|
// isAlive = true;
|
||
|
// isBack = false;
|
||
|
// });
|
||
|
// onDeactivated(() => {
|
||
|
// isAlive = true;
|
||
|
// isBack = false;
|
||
|
// });
|
||
|
|
||
|
return {
|
||
|
navigateBack,
|
||
|
navigateBackV2,
|
||
|
};
|
||
|
}
|