push
This commit is contained in:
45
lib/use/use-navigate.ts
Normal file
45
lib/use/use-navigate.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user