欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue使用element-ui實(shí)現(xiàn)菜單導(dǎo)航

 更新時(shí)間:2021年09月27日 11:43:15   作者:簡(jiǎn)單碼  
這篇文章主要為大家詳細(xì)介紹了Vue使用element-ui實(shí)現(xiàn)菜單導(dǎo)航,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Vue使用element-ui實(shí)現(xiàn)菜單導(dǎo)航的具體代碼,供大家參考,具體內(nèi)容如下

效果圖

目錄截圖

安裝vue-router 和 element-ui

vue-route是官方路由導(dǎo)航,element-ui是餓了么封裝的基于vue的組件庫(kù)

npm install vue-router --save
npm install element-ui --save

關(guān)閉ESLint檢查

新增配置文件src/vue.config.js 文件

module.exports = {
    lintOnSave: false
}

src/main.js

在main.js里引入vue-router 和 element-ui。
創(chuàng)建兩個(gè)頁(yè)面組件,電影和小說(shuō)。
定義路由映射。
路由改成h5模式,去掉難看的#符號(hào)。

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. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過(guò)先這么簡(jiǎn)單著吧。
const router = new VueRouter({
  mode: 'history',  //h5模式
  routes // (縮寫(xiě)) 相當(dāng)于 routes: routes
})

new Vue({
  render: h => h(App),
  router
}).$mount('#app')

src/comments/movie.vue

電影頁(yè)面組件

<template>
  <div>    
    movie頁(yè)面
  </div>
</template>

<script>
export default {
  name: 'movie' 
}
</script>


<style scoped>

</style>

src/comments/novel.vue

小說(shuō)頁(yè)面組件

<template>
  <div>
    novel頁(yè)面
  </div>
</template>

<script>
export default {
  name: 'novel'  
}
</script>
<style scoped>

</style>

src/comments/NavMenu.vue

導(dǎo)航組件。這里使用了element-ui的菜單組件。navMenuData模擬了我們菜單的數(shù)據(jù)。屬性default-active代表當(dāng)前選中的菜單,router屬性代表index自動(dòng)當(dāng)成路由路徑。

v-for循環(huán)生成菜單,在template標(biāo)簽中寫(xiě)v-for,不會(huì)一直復(fù)制當(dāng)前的template。

看別人博客都是:default-active="$route.path",我這里多了個(gè)/。所以在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">小說(shuō)</el-menu-item>
      <el-submenu index="3">
        <template slot="title">我的工作臺(tái)</template>
        <el-menu-item index="3-1">選項(xiàng)1</el-menu-item>
        <el-menu-item index="3-2">選項(xiàng)2</el-menu-item>
        <el-menu-item index="3-3">選項(xiàng)3</el-menu-item>
        <el-submenu index="3-4">
          <template slot="title">選項(xiàng)4</template>
          <el-menu-item index="3-4-1">選項(xiàng)1</el-menu-item>
          <el-menu-item index="3-4-2">選項(xiàng)2</el-menu-item>
          <el-menu-item index="3-4-3">選項(xiàng)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: "小說(shuō)" },
        {
          index: "2",
          name: "我的工作臺(tái)",
          child: [{ index: "2-1", name: "選項(xiàng)1" },{ index: "2-2", name: "選項(xiàng)2" },{ index: "2-3", name: "選項(xiàng)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的容器布局,引入了自己寫(xiě)的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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 探討Vue.js的組件和模板

    探討Vue.js的組件和模板

    指令是Vue.js中一個(gè)重要的特性, 主要提供了一種機(jī)制將數(shù)據(jù)的變化映射為DOM行為。下面通過(guò)本文給大家分享Vue.js的組件和模板,需要的朋友參考下吧
    2017-10-10
  • LRU算法在Vue內(nèi)置組件keep-alive中的使用

    LRU算法在Vue內(nèi)置組件keep-alive中的使用

    LRU算法全稱(chēng)為least recently use 最近最少使用,核心思路是最近被訪問(wèn)的以后被訪問(wèn)的概率會(huì)變高,那么可以把之前沒(méi)被訪問(wèn)的進(jìn)行刪除,維持一個(gè)穩(wěn)定的最大容量值,從而不會(huì)導(dǎo)致內(nèi)存溢出。
    2021-05-05
  • 詳解vue+css3做交互特效的方法

    詳解vue+css3做交互特效的方法

    本篇文章主要介紹了詳解vue+css3做交互特效的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • vue?實(shí)現(xiàn)滑動(dòng)塊解鎖示例詳解

    vue?實(shí)現(xiàn)滑動(dòng)塊解鎖示例詳解

    這篇文章主要為大家介紹了vue?實(shí)現(xiàn)滑動(dòng)塊解鎖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • vue3使用base64加密的兩種方法舉例

    vue3使用base64加密的兩種方法舉例

    這篇文章主要給大家介紹了關(guān)于vue3使用base64加密的兩種方法,我們?cè)趘ue項(xiàng)目中有時(shí)會(huì)使用到Base6464轉(zhuǎn)碼,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • vue cli構(gòu)建的項(xiàng)目中請(qǐng)求代理與項(xiàng)目打包問(wèn)題

    vue cli構(gòu)建的項(xiàng)目中請(qǐng)求代理與項(xiàng)目打包問(wèn)題

    這篇文章主要介紹了vue cli構(gòu)建的項(xiàng)目中請(qǐng)求代理與項(xiàng)目打包問(wèn)題,需要的朋友可以參考下
    2018-02-02
  • Vue實(shí)現(xiàn)動(dòng)態(tài)查詢(xún)規(guī)則生成組件

    Vue實(shí)現(xiàn)動(dòng)態(tài)查詢(xún)規(guī)則生成組件

    今天我們來(lái)給大家介紹下在Vue開(kāi)發(fā)中我們經(jīng)常會(huì)碰到的一種需求場(chǎng)景,本文主要介紹了Vue動(dòng)態(tài)查詢(xún)規(guī)則生成組件,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • element使用自定義icon圖標(biāo)的詳細(xì)步驟

    element使用自定義icon圖標(biāo)的詳細(xì)步驟

    前端經(jīng)常會(huì)用到UI提供的各種圖表,推薦阿里的圖標(biāo)庫(kù),下面這篇文章主要給大家介紹了關(guān)于element使用自定義icon圖標(biāo)的詳細(xì)步驟,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Axios代理配置及封裝響應(yīng)攔截處理方式

    Axios代理配置及封裝響應(yīng)攔截處理方式

    這篇文章主要介紹了Axios代理配置及封裝響應(yīng)攔截處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)示例代碼

    vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)示例代碼

    大多數(shù)后臺(tái)系統(tǒng)中都存在打印的需求,在有打印需求時(shí),對(duì)前端來(lái)說(shuō)當(dāng)然是直接打印頁(yè)面更容易,下面這篇文章主要給大家介紹了關(guān)于vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01

最新評(píng)論