创建goods文件夹,创建商品分类页面
初始化:
<template>
<div>
商品分类
</div>
</template>
<script>
export default{
data() {
return {
}
},
created() {
},
methods: {
},
}
</script>
<style lang="less" scoped>
</style>
路由导入:
import Cate from '../components/goods/Cate.vue'
{
path:'/categories',
component:Cate,
}
import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入路由
import Login from '../components/login.vue'
// 引入组件
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/users.vue'
import rights from '../components/power/rights.vue'
import roles from '../components/power/roles.vue'
import Cate from '../components/goods/Cate.vue'
Vue.use(VueRouter)
const routes = [
{ // 定义路由规则
path: '/login',
component: Login
},
{ // 路由重定向
path: '/',
redirect: '/login'
},
{
path: '/home',
component: home,
redirect:'/welcome',//重定向
children:[
{
path:'/welcome',
component:welcome,
},
{
path:'/users',
component:users,
},
{
path:'/rights',
component:rights,
},
{
path:'/roles',
component:roles,
},
{
path:'/categories',
component:Cate,
}
]
}
]
const router = new VueRouter({
routes
})
// 挂载路由守卫
// from路径起点 to路径终点 next是一个韩式表示放行 next()直接放行 next('/login')表示强制跳转
router.beforeEach((to, from, next) => {
if (to.path === '/login') return next() // 如果用户访问登录页 就直接放行
// 获取sessionStorage上的token
const tokenStr = window.sessionStorage.getItem('token')
if (!tokenStr) return next('/login') // 不存在token
next()// 存在直接放行
})
export default router
实现效果