douyin/src/router/index.js
2024-04-02 11:59:13 +08:00

54 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {createRouter, createWebHistory} from "vue-router";
import routes from "./routes";
import {useBaseStore} from "@/store/pinia";
const router = createRouter({
history: createWebHistory(),
routes,
scrollBehavior(to, from, savedPosition) {
// console.log('savedPosition', savedPosition)
if (savedPosition) {
return savedPosition
} else {
return {top: 0}
}
},
})
router.beforeEach((to, from) => {
const baseStore = useBaseStore()
//footer下面的5个按钮对跳不要用动画
let noAnimation = ['/', '/home', '/me', '/shop', '/message', '/publish', '/home/live', '/test']
if (noAnimation.indexOf(from.path) !== -1 && noAnimation.indexOf(to.path) !== -1) {
return true
}
const toDepth = routes.findIndex(v => v.path === to.path)
const fromDepth = routes.findIndex(v => v.path === from.path)
// const fromDepth = routeDeep.indexOf(from.path)
if (toDepth > fromDepth) {
if (to.matched && to.matched.length) {
let toComponentName = to.matched[0].components.default.name
// store.commit('updateExcludeRoutes', {type: 'remove', value: toComponentName})
baseStore.updateExcludeRoutes({type: 'remove', value: toComponentName})
// console.log('to', toComponentName)
// console.log('前进')
// console.log('删除', toComponentName)
}
} else {
if (from.matched && from.matched.length) {
let fromComponentName = from.matched[0].components.default.name
// store.commit('updateExcludeRoutes', {type: 'add', value: fromComponentName})
baseStore.updateExcludeRoutes({type: 'add', value: fromComponentName})
// console.log('后退')
// console.log('添加', fromComponentName)
}
}
// ...
// 返回 false 以取消导航
return true
})
export default router