Element-ui之導航選單

MrNow發表於2019-04-29

0. 總結

1. 選單 <el-menu> 包裹子選單 <el-submenu>
2. 顯示出來的層級關係由 index 的值控制:1 1-1 1-1-1 分別為三層
複製程式碼

1. 安裝並使用 element-ui

安裝

npm i element-ui
複製程式碼

使用

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI);
複製程式碼

2. 頂部導航

Element-ui之導航選單

<!-- 
  default-active: 當前啟用的選單 index
  mode: 預設 mode 為 vertical,更改為 horizontal 為水平選單
  background-color: 選單背景色
  text-color: 選單文字色
  active-text-color: 啟用的選單文字顏色
 -->
<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.baidu.com" target="blank">百度</a></el-menu-item>
</el-menu>
複製程式碼
data(){
    return {
      activeIndex: '1',
      activeIndex2: '1'
    }
  }
複製程式碼

3. 側邊欄

<template>
  <div class="home">
    <!-- 
      側邊欄 
    -->
    <el-menu
      default-active="2"
      background-color="#545c64"
      active-text-color="#ffd04b"
      text-color="#fff"
      @open="handleOpen"
      @close="handleClose"
    >
      <!-- 
        el-submenu: 子選單
        index: key: 1 Path: ["1"]
       -->
      <el-submenu index="1">
        <!-- 
          slot="title" 選單欄
        -->
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>導航一</span>
        </template>
        <!-- 分組 -->
        <el-menu-item-group>
          <template slot="title">分組一</template>
          <el-menu-item index="1-1">選項1</el-menu-item>
          <el-menu-item index="1-2">選項2</el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="分組2">
          <el-menu-item index="1-3">選項3</el-menu-item>
        </el-menu-item-group>
        <el-submenu index="1-4">
          <template slot="title">選項4</template>
          <el-menu-item index="1-4-1">選項1</el-menu-item>
        </el-submenu>
      </el-submenu>
      <el-menu-item index="2">
        <i class="el-icon-menu"></i>
        <span slot="title">導航二</span>
      </el-menu-item>
      <el-menu-item index="3" disabled="">
        <i class="el-icon-document"></i>
        <span>導航三</span>
      </el-menu-item>
      <el-menu-item index="3">
        <i class="el-icon-setting"></i>
        <span slot="title">導航四</span>
      </el-menu-item>
    </el-menu>
  </div>
</template>

<script>
// @ is an alias to /src


export default {
  name: 'home',
  components: {

  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  }
}
</script>
複製程式碼

具體參考官方文件

相關文章