最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3中使用ant-design-vue的layout組件實現(xiàn)動態(tài)導(dǎo)航欄和面包屑功能

 更新時間:2023年01月29日 14:48:52   作者:光年在眼前  
這篇文章主要介紹了vue3中使用ant-design-vue的layout組件實現(xiàn)動態(tài)導(dǎo)航欄和面包屑功能,基于一個新建的Vue3項目上實現(xiàn),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

0 前言

        最近在自己搞一個前后端小項目,前端想使用ant-design-vue的layout組件實現(xiàn)動態(tài)導(dǎo)航欄和面包屑,但是網(wǎng)上的資料較少,所以我就自己整合實現(xiàn)了一下,在此記錄分享。

1 準(zhǔn)備工作

        基于一個新建的Vue3項目上實現(xiàn)。

1.1 安裝ant-design-vue

        官方文檔:Components Overview - Ant Design Vue (antdv.com)

        安裝:

npm i --save ant-design-vue

        全局注冊:

import { createApp } from 'vue';
import Antd from 'ant-design-vue';
import App from './App';
import 'ant-design-vue/dist/antd.css';
 
const app = createApp(App);
 
app.use(Antd).mount('#app');

1.2 安裝圖標(biāo)組件包

npm install --save @ant-design/icons-vue

        main.js中引用并全局注冊

import * as Icons from '@ant-design/icons-vue'
//全局注冊圖標(biāo)
const icons = Icons
for (const i in icons) {
  app.component(i, icons[i])
}

2 選擇組件

        如下圖所示,復(fù)制組件代碼:

3 路由文件

        router/index.js文件

import { createRouter, createWebHashHistory } from 'vue-router'
 
const routes = [
  {
    //導(dǎo)航頁
    path: '/layout',
    name: 'layout',
    meta: {
      title: '首頁',
      keepalive: true
    },
    component: () => import('@/views/layout/'),
    children: [
      {
        //歡迎頁
        path: '/layout',
        name: 'welcome',
        meta: {
          title: '首頁',
          keepalive: true
        },
        component: () => import('@/views/welcome/')
      },
      {
        //實時數(shù)據(jù)
        path: '/runtimeData',
        name: 'runtimeData',
        meta: {
          title: '實時數(shù)據(jù)',
          keepalive: true
        },
        component: () => import('@/views/runtimeData/')
      },
      {
        //數(shù)據(jù)分析
        path: '/dataAnalysis',
        name: 'dataAnalysis',
        meta: {
          title: '數(shù)據(jù)分析',
          keepalive: true
        },
        component: () => import('@/views/dataAnalysis/')
      },
      {
        //數(shù)據(jù)處理(增刪改查)
        path: '/dataManage',
        name: 'dataManage',
        meta: {
          title: '數(shù)據(jù)總覽',
          keepalive: true
        },
        component: () => import('@/views/dataManage/')
      },
      {
        //查看用戶信息
        path: '/showUserInfo',
        name: 'showUserInfo',
        meta: {
          title: '查看用戶信息',
          keepalive: true
        },
        component: () => import('@/views/my/showUserInfo.vue')
      },
      {
        //修改用戶信息
        path: '/editUserInfo',
        name: 'editUserInfo',
        meta: {
          title: '修改用戶信息',
          keepalive: true
        },
        component: () => import('@/views/my/editUserInfo.vue')
      },
    ]
  },
  {
    //登錄頁面
    path: '/login',
    name: 'login',
    meta: {
      title: '登錄',
      keepalive: true
    },
    component: () => import('@/views/login/index.vue')
  },
 
]
 
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
 
export default router

4 Vue導(dǎo)航頁面

        views/layout/index.vue,主要關(guān)注標(biāo)簽a-layout中的內(nèi)容及相關(guān)變量

<template>
  <a-layout id="components-layout-demo-custom-trigger" style="min-height: 100vh">
    <a-layout-sider v-model:collapsed="collapsed" collapsible>
 
      <div class="logo">
        溫濕度數(shù)據(jù)顯示
      </div>
 
 
      <a-menu @click="navClick"
              v-model="currentSelectChild"
              @openChange="onOpenChange"
              :openKeys="currentParent"
              :inline-collapsed="collapsed"
              :selectedKeys="[route.path]"
              theme="dark"
              mode="inline"
      >
 
        <template v-for='(item,index) in NavDataInfo.NavData'>
          <a-sub-menu :key="item.Path" v-if='item.Child.length > 0'>
            <template #title>
              <a-icon>
                <meh-outlined/>
              </a-icon>
              <span>{{item.Title}}</span>
            </template>
            <a-menu-item v-for="(itChild,ind) in item.Child" :key="itChild.Path">
              <a-icon>
                <meh-outlined/>
              </a-icon>
              <router-link :to="itChild.Path">
                <!--根據(jù)路徑去跳轉(zhuǎn)頁面-->
                {{itChild.Title}}
              </router-link>
            </a-menu-item>
          </a-sub-menu>
 
 
          <a-menu-item :key="item.Path" v-else>
            <a-icon>
              <meh-outlined/>
            </a-icon>
            <router-link :to="item.Path">
              <a-icon :type="item.Icons"/>
              <span>{{item.Title}}</span>
            </router-link>
          </a-menu-item>
 
        </template>
 
      </a-menu>
 
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <div id="header">
          <div id="left">
            <span>作者:</span>
          </div>
          <div id="right">
            <a-avatar src="https://joeschmoe.io/api/v1/random"/>
            <el-dropdown>
              <span class="el-dropdown-link">
                User
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item><router-link to="/showUserInfo">我的信息</router-link></el-dropdown-item>
                  <el-dropdown-item><router-link to="/editUserInfo">修改信息</router-link></el-dropdown-item>
                  <el-dropdown-item><span @click="outLogin">退出登錄</span></el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </a-layout-header>
      <!--      <keep-alive>-->
      <a-layout-content style="margin: 0 16px">
        <!-- 面包屑 -->
        <a-breadcrumb style="margin: 16px 0" separator=">">
          <!-- 自定義返回函數(shù) ←-->
          <a-breadcrumb-item @click="goback">
            <a-icon>
              <import-outlined/>
            </a-icon>
            返回
          </a-breadcrumb-item>
          <a-breadcrumb-item v-for="(item, index) in breadList" :key="item.name">
            <router-link v-if="item.name !== name && index !== 1"
                         :to="{ path: item.path === '' ? '/' : item.path }">
              {{ item.meta.title }}
            </router-link>
            <span v-else>
              {{ item.meta.title }}
            </span>
          </a-breadcrumb-item>
        </a-breadcrumb>
        <!--          <transition>-->
        <div :style="{ padding: '24px', background: '#fff', minHeight: '100%' }">
          <router-view/>
        </div>
        <!--          </transition>-->
      </a-layout-content>
      <!--      </keep-alive>-->
      <a-layout-footer style="text-align: center">
        Great Project ?2022 Created by 
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>
<script setup>
  import { ref, reactive, onBeforeMount, watch, createVNode } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import { Modal, message } from 'ant-design-vue'
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
  import myRouter from '@/router'
  import { ArrowDown } from '@element-plus/icons-vue'
  //************************************************data部分
  const route = useRoute()
  const router = useRouter()
  const collapsed = ref(false)
  const selectedKeys = ref(['0'])
  const name = ref('')
  const breadList = ref([])
  const NavDataInfo = reactive({
    NavData: [
      {
        NavID: '0',
        Icons: 'home',
        Title: '首頁',
        Path: '/layout',
        Child: []
      },
      {
        NavID: '1',
        Icons: 'meh',
        Title: '實時數(shù)據(jù)',
        Path: '/runtimeData',
        Child: []
      },
      {
        NavID: '2',
        Icons: 'like',
        Title: '數(shù)據(jù)管理',
        Path: '/dataManage',
        Child: [
          {
            NavID: '2-1',
            Icons: 'man',
            Title: '數(shù)據(jù)總覽',
            Path: '/dataManage',
            Child: []
          },
        ]
      },
      {
        NavID: '3',
        Icons: 'key',
        Title: '數(shù)據(jù)分析',
        Path: '/dataAnalysis',
        Child: []
      },
    ],
  })
  //************************************************data部分
 
  //************************************************方法
  const getBreadcrumb = () => {
    breadList.value = []
    name.value = route.name
    route.matched.forEach(item => {
      breadList.value.push(item)
    })
    console.log(breadList.value)
    console.log(name.value)
    console.log(route)
 
  }
  // 返回上一頁,調(diào)用的組件 router.back();
  const goback = () => {
    //點擊了返回按鈕
    router.back()
  }
 
  //退出登錄
  const outLogin = () => {
    Modal.confirm({
      title: '您確定要退出登錄嗎?',
      icon: createVNode(ExclamationCircleOutlined),
      content: createVNode('div', {
        style: 'color:red;',
      }, '點擊OK則將退出!'),
      onOk () {
        // console.log('OK', key)
        message.success('退出登錄!')
        myRouter.push({ path: "/login" });
      },
      onCancel () {
        // console.log('Cancel')
        message.success('取消成功!')
      },
      class: 'test',
    })
  }
 
  //監(jiān)視路由
  watch(() => route.path, getBreadcrumb)
 
  //*************************************************方法
 
  //*************************************************生命周期
  onBeforeMount(() => {
    getBreadcrumb()
  })
  //*************************************************生命周期
 
 
</script>
<style scoped>
  #components-layout-demo-custom-trigger {
    height: 100%;
  }
 
  #components-layout-demo-custom-trigger .trigger {
    font-size: 18px;
    line-height: 64px;
    padding: 0 24px;
    cursor: pointer;
    transition: color 0.3s;
  }
 
  #components-layout-demo-custom-trigger .trigger:hover {
    color: #1890ff;
  }
 
  #components-layout-demo-custom-trigger .logo {
    height: 32px;
    background: rgb(127, 252, 255);
    margin: 16px;
    font-size: 20px;
    text-align: center;
    font-family: 宋體;
  }
 
  #header {
    display: flex;
    height: 70px;
    /*margin: 0;*/
    padding: 0;
  }
 
  #left {
    width: 80%;
    /*height: 25px;*/
    /*background-color: darksalmon;*/
    justify-content: flex-start;
    display: flex;
    align-items: center;
    margin-left: 16px;
  }
 
  #right {
    flex: 1;
    width: 20%;
    /*background-color: coral;*/
    /*height: 50px;*/
    justify-content: flex-end;
    display: flex;
    align-items: center;
    margin-right: 16px;
  }
  .example-showcase .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
  }
 
</style>

        上面的代碼中將路由文件中的路由表重新寫了一個變量,主要是為了方便,并不是所有頁面路由都要制作導(dǎo)航欄,這樣就不用在router/index.js中添加路由時考慮太多。

5 最終效果

         效果如上圖所示,我這里也寫了一個面包屑,不過還有些問題,就交給大伙兒實現(xiàn)吧!

到此這篇關(guān)于vue3中使用ant-design-vue的layout組件實現(xiàn)動態(tài)導(dǎo)航欄和面包屑功能的文章就介紹到這了,更多相關(guān)vue3使用ant-design-vue實現(xiàn)動態(tài)導(dǎo)航欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實現(xiàn)購物車列表

    vue實現(xiàn)購物車列表

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)購物車列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 基于Vue實現(xiàn)文件拖拽上傳功能

    基于Vue實現(xiàn)文件拖拽上傳功能

    文件拖拽上傳功能現(xiàn)在已經(jīng)隨處可見,大家應(yīng)該都用過了吧,那么它具體是怎么實現(xiàn)的大家有去了解過嗎,今天我們一起來實現(xiàn)一下這個功能,并封裝一個拖拽上傳組件吧
    2024-03-03
  • vue封裝動態(tài)表格方式詳解

    vue封裝動態(tài)表格方式詳解

    這篇文章主要為大家介紹了vue封裝動態(tài)表格方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Vue3中的組合式?API示例詳解

    Vue3中的組合式?API示例詳解

    組合式 API 是一系列 API 的集合,使我們可以使用函數(shù)而不是聲明選項的方式書寫 Vue 組件,這篇文章主要介紹了什么是Vue3的組合式?API,需要的朋友可以參考下
    2022-06-06
  • vue實現(xiàn)移動端H5數(shù)字鍵盤組件使用詳解

    vue實現(xiàn)移動端H5數(shù)字鍵盤組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)移動端H5數(shù)字鍵盤組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • vue解決花括號數(shù)據(jù)綁定不成功的問題

    vue解決花括號數(shù)據(jù)綁定不成功的問題

    今天小編就為大家分享一篇vue解決花括號數(shù)據(jù)綁定不成功的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue生命周期和react生命周期對比【推薦】

    vue生命周期和react生命周期對比【推薦】

    本文通過實例代碼給大家介紹了vue生命周期和react生命周期對比 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • Vue-路由導(dǎo)航菜單欄的高亮設(shè)置方法

    Vue-路由導(dǎo)航菜單欄的高亮設(shè)置方法

    下面小編就為大家分享一篇Vue-路由導(dǎo)航菜單欄的高亮設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue給數(shù)組中對象排序 sort函數(shù)的用法

    vue給數(shù)組中對象排序 sort函數(shù)的用法

    這篇文章主要介紹了vue給數(shù)組中對象排序 sort函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue使用自定義指令實現(xiàn)一鍵復(fù)制功能

    vue使用自定義指令實現(xiàn)一鍵復(fù)制功能

    在Vue中,通過自定義指令v-copy和document.execCommand方法,可以實現(xiàn)點擊按鈕復(fù)制內(nèi)容到剪貼板的功能,適用于處理長文本或多行文本的復(fù)制需求,而readonly屬性可避免內(nèi)容被修改和移動設(shè)備上的虛擬鍵盤干擾,感興趣的朋友一起看看吧
    2024-09-09

最新評論

阿荣旗| 河池市| 上虞市| 满洲里市| 屏南县| 会同县| 若羌县| 尼勒克县| 呼和浩特市| 恩平市| 加查县| 思茅市| 秭归县| 彭山县| 河津市| 铁岭市| 抚远县| 扎赉特旗| 娱乐| 西盟| 沛县| 扎囊县| 普陀区| 左贡县| 普宁市| 永州市| 永城市| 临泉县| 宁安市| 霸州市| 寻甸| 福泉市| 朝阳县| 乐至县| 南安市| 顺平县| 瑞昌市| 白玉县| 台湾省| 敖汉旗| 长泰县|