使用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>
相关推荐
Мартин.3 小时前
[Meachines] [Easy] Sea WonderCMS-XSS-RCE+System Monitor 命令注入
前端·xss
一 乐4 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
昨天;明天。今天。4 小时前
案例-表白墙简单实现
前端·javascript·css
数云界4 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
风清扬_jd4 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
安冬的码畜日常4 小时前
【玩转 JS 函数式编程_006】2.2 小试牛刀:用函数式编程(FP)实现事件只触发一次
开发语言·前端·javascript·函数式编程·tdd·fp·jasmine
ChinaDragonDreamer4 小时前
Vite:为什么选 Vite
前端
小御姐@stella4 小时前
Vue 之组件插槽Slot用法(组件间通信一种方式)
前端·javascript·vue.js
GISer_Jing4 小时前
【React】增量传输与渲染
前端·javascript·面试
GISer_Jing4 小时前
WebGL在低配置电脑的应用
javascript