<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Vue使用element-ui實現選單導航的具體程式碼,供大家參考,具體內容如下
效果圖
目錄截圖
安裝vue-router 和 element-ui
vue-route是官方路由導航,element-ui是餓了麼封裝的基於vue的元件庫
npm install vue-router --save npm install element-ui --save
關閉ESLint檢查
新增組態檔src/vue.config.js 檔案
module.exports = { lintOnSave: false }
src/main.js
在main.js裡引入vue-router 和 element-ui。
建立兩個頁面元件,電影和小說。
定義路由對映。
路由改成h5模式,去掉難看的#符號。
import Vue from 'vue' import App from './App.vue' import VueRouter from 'vue-router' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import movie from './components/movie.vue' import novel from './components/novel.vue' Vue.config.productionTip = false Vue.use(VueRouter) Vue.use(ElementUI); const routes = [ { path: '/movie', component: movie }, { path: '/novel', component: novel } ] // 3. 建立 router 範例,然後傳 `routes` 設定 // 你還可以傳別的設定引數, 不過先這麼簡單著吧。 const router = new VueRouter({ mode: 'history', //h5模式 routes // (縮寫) 相當於 routes: routes }) new Vue({ render: h => h(App), router }).$mount('#app')
src/comments/movie.vue
電影頁面元件
<template> <div> movie頁面 </div> </template> <script> export default { name: 'movie' } </script> <style scoped> </style>
src/comments/novel.vue
小說頁面元件
<template> <div> novel頁面 </div> </template> <script> export default { name: 'novel' } </script> <style scoped> </style>
src/comments/NavMenu.vue
導航元件。這裡使用了element-ui的選單元件。navMenuData模擬了我們選單的資料。屬性default-active代表當前選中的選單,router屬性代表index自動當成路由路徑。
v-for迴圈生成選單,在template標籤中寫v-for,不會一直複製當前的template。
看別人部落格都是:default-active="$route.path",我這裡多了個/。所以在mounted生命週期裡去除/。
<template> <div id="NavMenu"> <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" router > <!-- <el-menu-item index="1">電影</el-menu-item> <el-menu-item index="2">小說</el-menu-item> <el-submenu index="3"> <template slot="title">我的工作臺</template> <el-menu-item index="3-1">選項1</el-menu-item> <el-menu-item index="3-2">選項2</el-menu-item> <el-menu-item index="3-3">選項3</el-menu-item> <el-submenu index="3-4"> <template slot="title">選項4</template> <el-menu-item index="3-4-1">選項1</el-menu-item> <el-menu-item index="3-4-2">選項2</el-menu-item> <el-menu-item index="3-4-3">選項3</el-menu-item> </el-submenu> </el-submenu> --> <template v-for="item in navMenuData"> <el-menu-item :index="item.index" v-if="!item.child">{{item.name}}</el-menu-item> <el-submenu :index="item.index" v-if="item.child"> <template slot="title">{{item.name}}</template> <template v-for="item2 in item.child"> <el-menu-item :index="item2.index">{{item2.name}}</el-menu-item> </template> </el-submenu> </template> </el-menu> </div> </template> <script> export default { name: "NavMenu", data() { return { activeIndex: "movie", navMenuData: [ { index: "movie", name: "電影" }, { index: "novel", name: "小說" }, { index: "2", name: "我的工作臺", child: [{ index: "2-1", name: "選項1" },{ index: "2-2", name: "選項2" },{ index: "2-3", name: "選項3" }] }, ] }; }, methods: { handleSelect(key, keyPath) { console.log(key, keyPath); } }, mounted(){ console.log(this.activeIndex) console.log(this.$route.path) this.activeIndex = this.$route.path.substring(1,this.$route.path.length); } }; </script> <style scoped> </style>
src/App.vue
這裡使用了element-ui的容器佈局,引入了自己寫的NavMenu選單元件。
<template> <div id="app"> <el-container> <el-header> <NavMenu></NavMenu> </el-header> <el-main> <router-view></router-view> <!--路由出口 --> </el-main> <el-footer>Footer</el-footer> </el-container> </div> </template> <script> import NavMenu from "./components/NavMenu.vue"; export default { name: "app", components: { NavMenu } }; </script> <style scoped> .el-header, .el-footer { background-color: #b3c0d1; color: #333; text-align: center; height: 100px; padding: 0px; } .el-main { background-color: #e9eef3; color: #333; text-align: center; line-height: 160px; } </style>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45