优化路由

yzb
yzb 8 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 { import { createRouter, createWebHashHistory } from "vue-router";
createRouter,
createWebHashHistory
} from "vue-router";
import { userStore } from "./store"; import { userStore } from "./store";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes: [ routes: [
{ {
path: '/', path:'/',
redirect: '/login', redirect:{name:'admin'}
}, },
{
path: '/login',
name: 'Login',
component: () => import('./view/Login.vue'),
},
{ {
path: "/admin", path: "/admin",
name: 'admin', name: 'admin',
component: () => import('./view/user/index.vue'), component: () => import('./view/user/index.vue'),
redirect: '/admin/firstpage/1', // 默认重定向到 firstpage/1 redirect: { name: 'FirstPage' }, // Default redirect to firstpage route by name
children: [ children: [
{ {
path: 'student', path: 'student',
name: 'Student',
component: () => import('./view/user/student.vue'), component: () => import('./view/user/student.vue'),
}, },
{ {
path: 'teacher', path: 'teacher',
name: 'Teacher',
component: () => import('./view/user/teacher.vue'), component: () => import('./view/user/teacher.vue'),
}, },
{ {
path: 'information', path: 'information',
name: 'Information',
component: () => import('./view/user/information.vue'), component: () => import('./view/user/information.vue'),
}, },
{ {
path: 'myinfo', path: 'myinfo',
name: 'MyInfo',
component: () => import('./view/user/myinfo.vue'), component: () => import('./view/user/myinfo.vue'),
}, },
{ {
path: 'firstpage', path: 'firstpage',
name: 'FirstPage',
component: () => import('./view/user/firstpage.vue'), component: () => import('./view/user/firstpage.vue'),
children: [ children: [
{ {
path: '1', path: '1',
name: 'FirstPage1',
component: () => import('./components/1.vue'), component: () => import('./components/1.vue'),
}, },
{ {
path: '2', path: '2',
name: 'FirstPage2',
component: () => import('./components/2.vue'), component: () => import('./components/2.vue'),
}, },
{ {
path: '3', path: '3',
name: 'FirstPage3',
component: () => import('./components/3.vue'), component: () => import('./components/3.vue'),
}, },
{ {
path: '4', path: '4',
name: 'FirstPage4',
component: () => import('./components/4.vue'), component: () => import('./components/4.vue'),
} }
] ]
}, },
{ {
path: 'notice', path: 'notice',
name: 'Notice',
component: () => import('./view/user/notice.vue'), component: () => import('./view/user/notice.vue'),
} }
] ]
},
{
path: "/Register",
name: 'Register',
component: () => import('./view/Register.vue'),
} }
] ]
}); });
export default router;
// 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

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

Loading…
Cancel
Save