vue實(shí)現(xiàn)多級(jí)菜單效果
本次記錄基于iview3框架實(shí)現(xiàn)多級(jí)菜單+vue router實(shí)現(xiàn)頁面切換
方法一:
使用Tree 樹形控件,官方文檔
以官方demo為例,數(shù)據(jù)中添加URL屬性,用于路由跳轉(zhuǎn),正式項(xiàng)目中該tree控件的數(shù)據(jù)由后端給出,需要注意的是后端給出的URL跳轉(zhuǎn)地址最前一定要看清有沒有"/" ,如果沒有自己手動(dòng)加或后端改,沒有這個(gè)"/" 斜杠會(huì)導(dǎo)致路由跳轉(zhuǎn)失敗。
思路:根據(jù)官方文檔里面寫用on-select-change事件返回當(dāng)前已選中的節(jié)點(diǎn)數(shù)組、當(dāng)前項(xiàng),就利用返回的當(dāng)前項(xiàng)數(shù)據(jù)拿到URL,并使用router跳轉(zhuǎn)。
<template> <div class="layout"> <Layout> <Header> <Menu mode="horizontal" theme="dark" active-name="1"> <div class="layout-logo"></div> <div class="layout-nav"> <MenuItem name="1"> <Icon type="ios-navigate"></Icon> Item 1 </MenuItem> <MenuItem name="2"> <Icon type="ios-keypad"></Icon> Item 2 </MenuItem> <MenuItem name="3"> <Icon type="ios-analytics"></Icon> Item 3 </MenuItem> <MenuItem name="4"> <Icon type="ios-paper"></Icon> Item 4 </MenuItem> </div> </Menu> </Header> </Layout> <Layout style="height: 100%;width: 100%;"> <Sider hide-trigger breakpoint="md" width="200" :value=true> //方法一:使用Tree樹控件,綁定點(diǎn)選事件 <Tree :data="data1" @on-select-change="selectChange"></Tree> //方法二:使用menu導(dǎo)航菜單和遞歸 <!--<SubItem :model="item" :sindex="index" v-for="(item,index) in data1" :key="index"></SubItem>--> </Sider> <Layout > <router-view></router-view> </Layout> </Layout> </div> </template> <script> import SubItem from './SubItemm.vue' export default { components:{ SubItem }, data () { return { data1: [ { title: 'parent 1', expand: true, url:null, children: [ { title: 'parent 1-1', url:null, children: [ { title: 'leaf 1-1-1', url:'/chpo/chpo/chpoShow' }, { title: 'leaf 1-1-2', url:'/chpo/chpoCollection/chpocollectionshow' } ] }, { title: 'parent 1-2', url:null, children: [ { title: 'leaf 1-2-1', url:'/company/course/courseshow' }, { title: 'leaf 1-2-1', url:'/system/sysgamutgame/gamutgameshow' } ] } ] } ] } }, methods:{ selectChange(node,curr){ //node 當(dāng)前已選中的節(jié)點(diǎn)數(shù)組 //curr 當(dāng)前項(xiàng),這里可是拿到當(dāng)前項(xiàng)的數(shù)據(jù),這樣可以拿到跳轉(zhuǎn)的URL if(curr.url) this.$router.push(curr.url) } } } </script>
路由配置,這里子路由中的路徑要和后端給出的路由地址保持一致,才能正確跳轉(zhuǎn)
import Vue from 'vue' import Router from 'vue-router' import component1 from '@/components/component1' import component2 from '@/components/component2' import component3 from '@/components/component3' import component4 from '@/components/component4' import Index from '../view/Index' Vue.use(Router) export default new Router({ routes: [ { path: '/', name:'Index', component: Index, children:[ { path: '/chpo/chpo/chpoShow', name:'component1', component: component1 }, { path: '/chpo/chpoCollection/chpocollectionshow', name:'component2', component: component2 }, { path: '/company/course/courseshow', name:'component3', component: component3 }, { path: '/system/sysgamutgame/gamutgameshow', name:'component4', component: component4 }, ] }, ] })
方法二:
使用Menu 導(dǎo)航菜單和遞歸來實(shí)現(xiàn),組件官方文檔
思路:①根據(jù)官方文檔 MenuItem有 to 和 target 屬性,使用其一都能實(shí)現(xiàn)跳轉(zhuǎn),但跳轉(zhuǎn)結(jié)果可能不一樣,這里使用to屬性跳轉(zhuǎn)
②在子組件內(nèi)進(jìn)行是否為最終子項(xiàng),若不是則使用遞歸進(jìn)行再次循環(huán),直到最終子項(xiàng)
子組件
<template> <Submenu :name="model.title" style="width: 200px"> <template slot="title" style="width: 200px"> {{model.title}} </template> // v-if判斷是否為最終的子項(xiàng),如果是則進(jìn)行MenuItem渲染,否則進(jìn)行遞歸調(diào)用 <MenuItem :name="item.title" v-for="item in model.children" :to="item.url" v-if="!item.children || item.children.length==0" :key="item.index" style="width: 200px">{{item.title}}</MenuItem> //遞歸調(diào)用 <SubItem :model="item" v-if="item.children&&item.children.length!==0" v-for="(item,index) in model.children" :key="index"></SubItem> </Submenu> </template> <script> export default { name: "SubItem", //至關(guān)重要的一步,一定要寫name,遞歸的時(shí)候使用 props:['model'], } </script>
在父組件中調(diào)用,使用v-for循環(huán)組件,傳入當(dāng)前item值即可,調(diào)用的代碼已經(jīng)在上面寫過,不在贅述。
在MenuItem上綁定屬性:to 跳轉(zhuǎn)的router路徑,即可實(shí)現(xiàn)頁面切換。
最后截圖展示效果:
方法一:使用tree樹形組件效果
方法二:Menu組件和遞歸使用效果
至此,兩種方法寫完了,自己學(xué)習(xí)記錄,僅供參考思路。
更多教程點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue動(dòng)態(tài)路由緩存不相互影響的解決辦法
這篇文章主要介紹了Vue動(dòng)態(tài)路由緩存不相互影響的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Vue-router 中hash模式和history模式的區(qū)別
這篇文章主要介紹了Vue-router 中hash模式和history模式的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07vue實(shí)現(xiàn)form表單與table表格的數(shù)據(jù)關(guān)聯(lián)功能示例
這篇文章主要介紹了vue實(shí)現(xiàn)form表單與table表格的數(shù)據(jù)關(guān)聯(lián)功能,涉及vue.js表單事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01