element中el-menu的動態變化問題

<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
 <el-menu-item index="1">處理中心</el-menu-item>
 <el-submenu index="2">
 <template slot="title">我的工作臺</template>
 <el-menu-item index="2-1">選項1</el-menu-item>
 <el-menu-item index="2-2">選項2</el-menu-item>
 <el-menu-item index="2-3">選項3</el-menu-item>
 <el-submenu index="2-4">
  <template slot="title">選項4</template>
  <el-menu-item index="2-4-1">選項1</el-menu-item>
  <el-menu-item index="2-4-2">選項2</el-menu-item>
  <el-menu-item index="2-4-3">選項3</el-menu-item>
 </el-submenu>
 </el-submenu>
 <el-menu-item index="3" disabled>消息中心</el-menu-item>
 <el-menu-item index="4"><a href="https://www.ele.me" rel="external nofollow" target="_blank">訂單管理</a></el-menu-item>
</el-menu>
 
<script>
 export default {
	 data() {
		  return {
			  activeIndex: '1'
		  };
	 },
	 methods: {
	  handleSelect(key, keyPath) {
		  console.log(key, keyPath);
	  }
	 }
 }
</script>

注意屬性:

default-active:當前激活菜單的 index

router:是否使用 vue-router 的模式,啓用該模式會在激活導航時以 index 作爲 path 進行路由跳轉

index:唯一標誌,,類型爲字符串,在每一個el-menu-item組件上都有一個編號,給default-active標記

使用菜單欄進行路由跳轉:

<el-menu :default-active="this.$router.path" router mode="horizontal">
	 <el-menu-item v-for="(item,i) in navList" :key="i" :index="item.name">
	  {{ item.navItem }}
	 </el-menu-item>
</el-menu>

數據:

data() {
	  return {
		  navList:[
			   {name:'/findProject',navItem:'發現項目'},
			   {name:'/communityActivity',navItem:'社區動態'},
			   {name:'/publishProject',navItem:'發佈項目'},
			   {name:'/personalCenter',navItem:'個人中心'},
			   {name:'/manageCenter',navItem:'管理員中心'},
		  ]
	  }
 }

router.js

配置路由

使用菜單欄進行路由跳轉有幾個注意點:

  1. 在el-menu加上router

  2. index必須綁定路由的path,參考上面的例子,’/'不能少

  3. default-active設爲當前路由(this.$route.path),這樣在路由變化的時候,對應的menu-item纔會高亮

當this.$route.path等於el-menu-item標籤中的index屬性值時則該item爲當前項,對應的menu-item纔會高亮。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章