优化路由

yzb
yzb 7 months ago
parent f8be43ef0a
commit 0451e84634

Binary file not shown.

Before

Width:  |  Height:  |  Size: 271 KiB

After

Width:  |  Height:  |  Size: 205 KiB

@ -1,98 +1,75 @@
import {
createRouter,
createWebHashHistory
} from "vue-router";
import { createRouter, createWebHashHistory } from "vue-router";
import { userStore } from "./store";
import { storeToRefs } from "pinia";
const router = createRouter({
history: createWebHashHistory(),
routes: [
{
path: '/',
redirect: '/login',
},
{
path: '/login',
name: 'Login',
component: () => import('./view/Login.vue'),
},
{
path:'/',
redirect:{name:'admin'}
},
{
path: "/admin",
name: 'admin',
component: () => import('./view/user/index.vue'),
redirect: '/admin/firstpage/1', // 默认重定向到 firstpage/1
redirect: { name: 'FirstPage' }, // Default redirect to firstpage route by name
children: [
{
path: 'student',
name: 'Student',
component: () => import('./view/user/student.vue'),
},
{
path: 'teacher',
name: 'Teacher',
component: () => import('./view/user/teacher.vue'),
},
{
path: 'information',
name: 'Information',
component: () => import('./view/user/information.vue'),
},
{
path: 'myinfo',
name: 'MyInfo',
component: () => import('./view/user/myinfo.vue'),
},
{
path: 'firstpage',
name: 'FirstPage',
component: () => import('./view/user/firstpage.vue'),
children: [
{
path: '1',
name: 'FirstPage1',
component: () => import('./components/1.vue'),
},
{
path: '2',
name: 'FirstPage2',
component: () => import('./components/2.vue'),
},
{
path: '3',
name: 'FirstPage3',
component: () => import('./components/3.vue'),
},
{
path: '4',
name: 'FirstPage4',
component: () => import('./components/4.vue'),
}
]
},
{
path: 'notice',
name: 'Notice',
component: () => import('./view/user/notice.vue'),
}
]
},
{
path: "/Register",
name: 'Register',
component: () => import('./view/Register.vue'),
}
]
});
// router.beforeEach((to,from,next)=>{
// const store=userStore()
// const{
// loginState
// }=storeToRefs(store)
// console.log("路由守卫",loginState.value)
// if(to.name=="Login"){
// next()
// }else{
// if(loginState.value){
// next()
// }else{
// next({
// name:'Login'
// })
// }
// }
// })
export default router
export default router;

@ -47,8 +47,6 @@
import { ref } from 'vue';
import { useRouter } from 'vue-router';
const activeTab = ref('first');
const router = useRouter();
@ -56,19 +54,18 @@ function handleTabClick(tab) {
console.log('Tab clicked:', tab.name);
const tabRoutes = {
first: '/admin/firstpage/1', //
second: '/admin/firstpage/2',
third: '/admin/firstpage/3',
fourth: '/admin/firstpage/4'
first: { name: 'FirstPage1' }, // Navigate using named routes
second: { name: 'FirstPage2' },
third: { name: 'FirstPage3' },
fourth: { name: 'FirstPage4' }
};
const route = tabRoutes[tab.props.name]; // tab.props name
const route = tabRoutes[tab.props.name]; // Ensure correct name
if (route) {
router.push(route); //
router.push(route); // Use named route
}
}
const items = [
{ id: 1, src: "/1.jpg" },
{ id: 2, src: "/2.jpg" },
@ -94,14 +91,12 @@ const newsList = ref([
},
]);
function goToInformation() {
router.push('/admin/information'); // information
router.push({ name: 'Information' }); // Use named route
}
</script>
<style scoped>
.header {
text-align: center;

Loading…
Cancel
Save