本次记录基于iview3框架实现多级菜单+vue router实现页面切换
方法一:
使用Tree 树形控件,官方文档
以官方demo为例,数据中添加URL属性,用于路由跳转,正式项目中该tree控件的数据由后端给出,需要注意的是后端给出的URL跳转地址最前一定要看清有没有"/" ,如果没有自己手动加或后端改,没有这个"/" 斜杠会导致路由跳转失败。
思路:根据官方文档里面写用on-select-change事件返回当前已选中的节点数组、当前项,就利用返回的当前项数据拿到URL,并使用router跳转。
<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树控件,绑定点选事件
<Tree :data="data1" @on-select-change="selectChange"></Tree>
//方法二:使用menu导航菜单和递归
<!--<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 当前已选中的节点数组
//curr 当前项,这里可是拿到当前项的数据,这样可以拿到跳转的URL
if(curr.url)
this.$router.push(curr.url)
}
}
}
</script>
路由配置,这里子路由中的路径要和后端给出的路由地址保持一致,才能正确跳转
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 导航菜单和递归来实现,组件官方文档
思路:①根据官方文档 MenuItem有 to 和 target 属性,使用其一都能实现跳转,但跳转结果可能不一样,这里使用to属性跳转
②在子组件内进行是否为最终子项,若不是则使用递归进行再次循环,直到最终子项
子组件
<template>
<Submenu :name="model.title" style="width: 200px">
<template slot="title" style="width: 200px">
{{model.title}}
</template>
// v-if判断是否为最终的子项,如果是则进行MenuItem渲染,否则进行递归调用
<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>
//递归调用
<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", //至关重要的一步,一定要写name,递归的时候使用
props:['model'],
}
</script>
在父组件中调用,使用v-for循环组件,传入当前item值即可,调用的代码已经在上面写过,不在赘述。
在MenuItem上绑定属性:to 跳转的router路径,即可实现页面切换。
最后截图展示效果:
方法一:使用tree树形组件效果
方法二:Menu组件和递归使用效果
至此,两种方法写完了,自己学习记录,仅供参考思路。
更多教程点击《Vue.js前端组件学习教程》,欢迎大家学习阅读。
关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
vue,多级菜单
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。

