使用vue3+ts+vite从零开始搭建bolg(五):layout(持续更新中)

五、layout搭建

5.1静态搭建

在src下创建如图文件夹

这里用logo举例,在scripts里export

javascript 复制代码
  <script lang="ts">
  export default {
    name: 'Logo',
  }
  </script>

然后在layout里引入

javascript 复制代码
//引入左侧菜单顶部用户信息
import Logo from './logo/index.vue'

接着直接使用

javascript 复制代码
<!-- 顶部用户信息 -->
      <Logo class="logo"></Logo>

以此类推,完成layout的静态搭建

5.2动态菜单

首先按照上述方法引入菜单

javascript 复制代码
<el-menu
        text-color="white"
        background-color="#001529"
        :default-active="$route.path"
        :collapse="LayOutSettingStore.fold ? true : false"
      >
        <Menu class="menu"></Menu>
</el-menu>

然后把菜单放入userStore里

javascript 复制代码
import { constantRoute } from '@/router/routes'

state: () => {
    return {
      token: GET_TOKEN(),
      username: '',
      menuRoutes:constantRoute,
    }
  },

再在Menu里,通过prop传到子组件

javascript 复制代码
<Menu :menuList="userStore.menuRoutes" class="menu"></Menu>

//引入用户相关的小仓库
import useUserStore from '../store/modules/user'
let userStore = useUserStore()

在menu的index.vue,定义并遍历

javascript 复制代码
<template>
    <div>
        <template v-for="item in menuList" :key="item.path">
            
        </template>
    </div>
</template>


defineProps(['menuList'])
没有子路由

即路由表里没有子路由

javascript 复制代码
<!--没有子路由-->
      <template v-if="!item.children">
        <el-menu-item
          v-if="!item.meta.hidden"
          :index="item.path"
          @click="goRoute"
        >
          <el-icon>
            <component :is="item.meta.icon"></component>
          </el-icon>
          <template #title>
            <span>{{ item.meta.title }}</span>
          </template>
        </el-menu-item>
      </template>

页面跳转goRoute方法

javascript 复制代码
import { useRouter } from 'vue-router'

let $router = useRouter()
const goRoute = (vc: any) => {
  $router.push(vc.index)
}
有子路由但是只有一个
javascript 复制代码
<!-- 有子路由但是只有一个的 -->
      <template v-if="item.children && item.children.length == 1">
        <el-menu-item
          v-if="!item.children[0].meta.hidden"
          :index="item.children[0].path"
          @click="goRoute"
        >
          <el-icon>
            <component :is="item.children[0].meta.icon"></component>
          </el-icon>
          <template #title>
            <span>{{ item.children[0].meta.title }}</span>
          </template>
        </el-menu-item>
      </template>
有子路由但是子路由大于一

采用递归的方法

javascript 复制代码
      <el-sub-menu
        :index="item.path"
        v-if="item.children && item.children.length > 1"
      >
        <template #title>
          <el-icon>
            <component :is="item.meta.icon"></component>
          </el-icon>
          <span>{{ item.meta.title }}</span>
        </template>
        <Menu :menuList="item.children"></Menu>
      </el-sub-menu>
相关推荐
奋飛5 分钟前
TypeScript系列:第六篇 - 编写高质量的TS类型
javascript·typescript·ts·declare·.d.ts
老A技术联盟6 分钟前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游9 分钟前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte14 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟23 分钟前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor23 分钟前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js
FogLetter24 分钟前
初识图片懒加载:让网页像"懒人"一样聪明加载
前端·javascript
微客鸟窝25 分钟前
一文搞懂NVM管理Node.js:从安装到实战全攻略
前端
归于尽26 分钟前
Cookie、Session、JWT 的前世今生
前端
程序员辉哥27 分钟前
学会在Cursor中使用Rules生成代码后可以躺平了吗?
前端·后端