19-项目路由规则介绍

首先我们改一下mock里面的菜单数据,改一下 name,商品为 pms 以及 子菜单商品列表为 product

然后我们改一下 home 页面的渲染,一个是路由的 index 改用 name, 一个是右边栏

html 复制代码
<template>
    <div class="home-container">
        <div class="home-header">头部</div>
        <div class="home-menu">
            <el-menu
                active-text-color="#ffd04b"
                background-color="#545c64"
                class="el-menu-vertical-demo"
                default-active="2"
                text-color="#fff"
                :unique-opened="true"
                :router="true"
            >
                <el-sub-menu v-for="menu in newMenus" :index="menu.id">
                    <template #title>
                        <span>{{ menu.title }}</span>
                    </template>
                    <template v-for="submenu in menu.children">
                        <el-menu-item v-if="submenu.hidden === 0" :index="'/' + menu.name + '/' + submenu.name" :key="submenu.id">
                            {{ submenu.title }}
                        </el-menu-item>
                    </template>
                </el-sub-menu>
            </el-menu>
        </div>
        <div class="home-content">
            <router-view></router-view>
        </div>
    </div>
</template>

<script lang='ts' setup>
/**
{
    id: {
        title: "一级菜单",
        children: [
            {title: "二级菜单"},
            {title: "二级菜单"},
            {title: "二级菜单"},
            {title: "二级菜单"},
        ]
    }
}
 * */ 
import { computed } from 'vue'
import { useStore } from 'vuex'

interface MenuObj {
    id: string
    name: string
    title: string
    parentId: string
    hidden: 0 | 1
    children?: MenuObj[]
}

interface NewMenus {
    [key: string]: MenuObj
}

const store = useStore();
const newMenus = computed<NewMenus>(() => store.getters.getNewMenus);
console.log('newMenus---------home-----------', newMenus)
</script>

<style lang='less' scoped>
.home-container {
    position: relative;
    height: 100%;

    .home-header {
        height: 70px;
        background-color: goldenrod;
    }

    .home-menu {
        position: absolute;
        top: 70px;
        left: 0;
        bottom: 0;
        width: 250px;
        background-color: #a37676;
    }

    .home-content {
        position: absolute;
        top: 70px;
        right: 0;
        left: 250px;
        bottom: 0;
        background-color: skyblue;
    }
}
</style>

下面我们需要通过菜单接口返回的数据去组装路由的规则,先模拟一下规则

ts 复制代码
import {
    createRouter,
    createWebHashHistory,
    type RouteRecordRaw
} from 'vue-router'
import { type App } from 'vue'
import store from '../store'
import Cookie from 'js-cookie'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login/login.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('../views/home/home.vue')
    },
    // 动态生成的路由配置结构需要如下
    {
        path: '/pms',
        name: 'pms',
        component: () => import('../views/home/home.vue'),
        children: [
            {
                path: 'product',
                name: 'product',
                component: () => import('../views/pms/product.vue'),
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes // 路由配置
})

// 前置导航守卫
router.beforeEach((_to, _from, next) => {
    // 1、token && vuex里面的 menus 为空
    const token = Cookie.get('token')
    console.log(store)
    if(token && store.state.menus.length === 0) {
        console.log('menus为空')
        // 获取用户信息
        store.dispatch('getAdminInfoApi');
    }
    next()
})

export const initRouter = (app: App<Element>) => {
    app.use(router)
}

添加一下 pms/product 页面

html 复制代码
<template>
    <div class=''>
        商品
    </div>
</template>

<script lang='ts' setup>
import { } from 'vue'

</script>

<style lang='less' scoped>

</style>

点击商品列表就会跳转到 http://localhost:5173/#/pms/product,右侧就是展示商品页面

相关推荐
吴声子夜歌10 小时前
Vue3——网络框架Axios的应用
javascript·vue3·axios
赵庆明老师9 天前
vben开发入门6:tsconfig.json
json·vue3·vben
赵庆明老师9 天前
vben开发入门5:vite.config.ts
前端·html·vue3·vben
沙振宇10 天前
【Web】使用Vue3+PlayCanvas开发3D游戏(十二)渲染PCD点云可视化模型
3d·vue3·点云·pcd
是席木木啊13 天前
告别console.log!Vue3项目日志框架选型指南
前端·vue3·日志框架
程序员-南13 天前
解决 Vue3 中 keep-alive 缓存问题的方法
缓存·vue3
qq_120840937114 天前
Three.js 模型加载稳定性实战:从资源失败到可用发布的工程化方案
前端·javascript·vue.js·vue3·three.js
qq_120840937114 天前
Three.js 模型加载与线上稳定性实战:路径、跨域、缓存与降级全链路指南
开发语言·javascript·缓存·vue3
qq_120840937114 天前
Vue3 + Three.js 实战入门:从零搭建可交互3D场景(含模型加载与性能优化)
javascript·3d·vue3·交互
qq_120840937114 天前
Vue3 + Three.js 入门实战:从 0 到 1 搭建可交互的 3D 场景(含模型加载与性能优化)
javascript·3d·vue3·交互·webgl·gltf