title: 总结制作NavMenu导航菜单组件时遇到的问题date: 2018-03-23 14:22:51
tags: common
基于Element的NavMenu
1.下拉导航栏的实现
1.1.导航栏横向排列
在一级li标签的样式上作用此语句,即可实现横向顶端导航栏。
float: left;
1.2.二级下拉菜单实现隐藏效果
在二级ul标签的样式上作用此语句。
display: none;
2.利用Vue.js中的v-for指令循环制作导航栏
<el-menu :default-active="activeIndex" class="el-menu-demo m-menu-demo" mode="horizontal" @select="handleSelect" background-color="rgba(255,255,255, 0)" text-color="#fff" active-text-color="#ffd04b"> <el-submenu :index="''+index_f" :key="index" v-for="(item_f, index_f) in nav"> <template slot="title"><a>{{item_f.first}}</a></template> <el-menu-item :index="''+index_f+'-'+index_s" key="index" v-for="(item_s, index_s) in item_f.second"><a>{{item_s.name}}</a> </el-menu-item> </el-submenu></el-menu>
2.1.v-for指令的使用
格式:item in itemsitem: 指向的数组单元, items:数组名称可以有多个item,比如(item, index)中的item遍历内容,index遍历下标。而下标的作用是在index属性中用于实现唯一性,让每个二级下拉菜单单独显示。
2.2.Json存储v-for遍历的data
2.2.1.Json文件的制作
Json 在线解析及格式化验证 [http://www.json.cn/](http://www.json.cn/) 制作好的.json文件放置于static静态文件夹中。
2.2.2.Json文件的使用
1.axios
基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 安装——使用npm
$ npm install axios
组件中引用axios
import axios from 'axios'
2.预置变量
在组件的data下要有接受数据的预置变量,如:
nav: []
3.回调函数
created() { var _this = this; this.$nextTick(function () { _this.getData(); }) }
4.获取数据函数
getData() { axios.get('/static/json/nav.json').then((res) => { this.nav = res.data; }); }
3.CSS调整NavMenu样式
3.1.二级下拉菜单透明
在el-menu标签(包裹着一级ul标签的div标签)上设置:
background-color="rgba(255,255,255, 0)"
在二级ul标签上设置:
background: rgba(255, 255, 255, 0.8);
3.2.二级下拉菜单宽度自适应
在二级ul标签上设置:
min-width: initial;
3.3.二级下拉菜单阴影
在二级ul标签上设置:
box-shadow: 0 1px 15px #818383;
3.4.在Element组件中修改设置CSS样式
3.4.1.!important
在index.html文件中引用CSS样式表,样式表内部直接对F12检查获得的Class名称对应的样式进行覆盖,注 意,需要使用!important,如:
color: #fff !important;
3.4.2.style标签中设置
在对应组件中的style标签内设置class样式,再对对应的标签设置class,需要注意的是有些组件由于是封装好的,在解析以后出现的标签在解析之前是没有的,故不容易设置对应的样式,比如NavMenu。当然,这种方式下设置css样式的优先级要比预设要高,不需要添加!important。
4.XGZX官网NavMenu源码
<template> <div> <div class="header"> <router-link to="/" class="left"><img src="/static/left.png"/></router-link> <div class="navbar"> <el-menu :default-active="activeIndex" class="el-menu-demo m-menu-demo" mode="horizontal" @select="handleSelect" background-color="rgba(255,255,255, 0)" text-color="#fff" active-text-color="#ffd04b"> <el-submenu :index="''+index_f" :key="index" v-for="(item_f, index_f) in nav"> <template slot="title"><a>{{item_f.first}}</a></template> <el-menu-item :index="''+index_f+'-'+index_s" key="index" v-for="(item_s, index_s) in item_f.second"><a> {{item_s.name}}</a> </el-menu-item> </el-submenu> </el-menu> </div> <div class="right"><img src="/static/right.png"/></div> </div> <img src="/static/slide.png"> </div></template>
<script> import axios from 'axios' export default { name: "CommonHeader", data() { return { activeIndex: '1', nav: [] }; }, created() { var _this = this; this.$nextTick(function () { _this.getData(); }) }, methods: { handleSelect(key, keyPath) { console.log(key, keyPath); }, getData() { axios.get('/static/json/nav.json').then((res) => { this.nav = res.data; }); } } }</script>
<style scoped> .header { position: fixed; background: #0e1856; width: 100%; height: 100px; overflow: hidden; } .navbar { position: absolute; left: 300px; bottom: 0; min-width: 1100px; } .left { position: absolute; top: 20%; left: 18px; width: 200px; height: auto; margin: 0 30px; } .left img { width: 100%; } .right { position: absolute; top: 15%; right: 50px; width: 300px; height: auto; } .right img { width: 100%; }</style>
index.html文件下引用的css样式(使用!important)
/* CommonHeader_el-menu*/.el-menu-demo { border-bottom-width: 0px !important;}.el-submenu__title { font-size: 16px !important; border-bottom-width: 0 !important; padding: 0 20px !important; color: #fff !important;}.el-submenu__title:hover { color: #000 !important; background: #fff !important;}i.el-icon-arrow-down { display: none;}.el-menu-item { text-align: center !important; height: 40px !important; font-size: 15px !important; background: rgba(255, 255, 255, 0.8) !important; color: #000 !important; padding: 0 22px !important;}.el-menu-item a { white-space: nowrap; padding: 0;}.el-menu-item a:hover { text-decoration: none;}.el-menu-item a:link { text-decoration: none; color: #000;}.el-menu-item a:visited { text-decoration: none; color: #000;}.el-menu-item:hover { background: rgba(14, 24, 86, 0.8) !important; color: #fff !important;}.el-menu--popup { padding: 0 !important; min-width: initial !important; box-sizing: border-box; box-shadow: 0 1px 15px #818383 !important;}.el-menu--popup-bottom-start { margin-top: 0 !important;}