SPA项目开发之首页导航+左侧菜单

spa

mock.js的使用以及主界面搭建

mock.js模拟响应ajax请求

安装mock.js依赖

 npm install mockjs -D              #只在开发环境使用

右击打spa项目所在目录shift+右击选择命令窗口
SPA项目开发之首页导航+左侧菜单
成功:
SPA项目开发之首页导航+左侧菜单

如果失败就删掉spa项目重新下载

配置开发环境及生产环境
为了只在开发环境使用mock,而打包到生产环境时自动不使用mock,我们可以在env中做一个配置
决定使不使用
SPA项目开发之首页导航+左侧菜单
dev.env

'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  MOCK: 'true'
})

prod.env

'use strict'
module.exports = {
  NODE_ENV: '"production"',
  MOCK: 'false'
}

点击:资料的分享
放入要用的图片
SPA项目开发之首页导航+左侧菜单
导入main.js引用(此用的是工具包,资料有分享)

//开发环境下才会引入mockjs
process.env.MOCK && require('@/mock')
import 'element-ui/lib/theme-chalk/index.css' //在css上面引用
import App from './App'

一定放在APP之前

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
process.env.MOCK && require('@/mock')
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import axios from '@/api/http'   
// import axios from 'axios'     
import VueAxios from 'vue-axios'

Vue.use(ElementUI)
Vue.use(VueAxios,axios)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

注1:import是ES6标准中的模块化解决方案,require是node中遵循CommonJS规范的模块化解决方案
后者支持动态引入,也就是require(${path}/xx.js)

mock的使用

在src目录下创建mock目录,定义mock主文件index.js,并在该文件中定义拦截路由配置,
      /src/mock/index.js
      
      导入公共模块及mockjs全局设置
      import Mock from 'mockjs' //引入mockjs,npm已安装
      import action from '@/api/action' //引入封装的请求地址
 
      //全局设置:设置所有ajax请求的超时时间,模拟网络传输耗时
      Mock.setup({
// timeout: 400  //延时400s请求到数据
timeout: 200 - 400 //延时200-400s请求到数据
      })

注1:index.js文件的作用很显然,就是将分散的xxx-mock文件集合起来.后面再添加新的mock文件,都需要在这里引入

去App.vue运行测试结果:
SPA项目开发之首页导航+左侧菜单

后台主界面搭建

结构
SPA项目开发之首页导航+左侧菜单

导入组件(上面资料有分享)
SPA项目开发之首页导航+左侧菜单
路由配置src/router/index.js

import AppMain from '@/components/AppMain'

新增映射

{
			path: '/AppMain',
			name: 'AppMain',
			component: AppMain
		}

修改Login.vue的登录成功时跳主界面

	this.$router.push({path:'/AppMain'})

SPA项目开发之首页导航+左侧菜单

AppMain.vue为主界面,重载着LeftNav.vue和TopNav.vue组件

AppMain.vue

<template>
	<el-container class="main-container">
		<el-aside v-bind:class="asideClass">
			<LeftNav></LeftNav>
		</el-aside>
		<el-container>
			<el-header class="main-header">
				<TopNav></TopNav>
			</el-header>
			<el-main class="main-center">Main</el-main>
		</el-container>
	</el-container>
</template>

<script>
	// 导入组件
	import TopNav from '@/components/TopNav.vue'
	import LeftNav from '@/components/LeftNav.vue'

	// 导出模块
	export default {
		data(){
			return {
				asideClass:'main-aside'
			}
		},
		components: {
			TopNav,LeftNav
		},
	};
</script>
<style scoped>
	.main-container {
		height: 100%;
		width: 100%;
		box-sizing: border-box;
	}

	.main-aside-collapsed {
		/* 在CSS中,通过对某一样式声明! important ,可以更改默认的CSS样式优先级规则,使该条样式属性声明具有最高优先级 */
		width: 64px !important;
		height: 100%;
		background-color: #334157;
		margin: 0px;
	}

	.main-aside {
		width: 240px !important;
		height: 100%;
		background-color: #334157;
		margin: 0px;
	}

	.main-header,
	.main-center {
		padding: 0px;
		border-left: 2px solid #333;
	}
</style>


LeftNav.vue

<template>
	<el-menu default-active="2" class="el-menu-vertical-demo" background-color="#334157"
	 text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed" >
		<!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
		<div class="logobox">
			<img class="logoimg" src="../assets/img/logo.png" alt="">
		</div>
		<el-submenu index="1">
			<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 slot="title">导航三</span>
		</el-menu-item>
		<el-menu-item index="4">
			<i class="el-icon-setting"></i>
			<span slot="title">导航四</span>
		</el-menu-item>
	</el-menu>
</template>
<script>
	export default {
		data() {
			return {
				//默认不折叠
				collapsed: false
			}
		}
	}
</script>
<style>
	.el-menu-vertical-demo:not(.el-menu--collapse) {
		width: 240px;
		min-height: 400px;
	}

	.el-menu-vertical-demo:not(.el-menu--collapse) {
		border: none;
		text-align: left;
	}

	.el-menu-item-group__title {
		padding: 0px;
	}

	.el-menu-bg {
		background-color: #1f2d3d !important;
	}

	.el-menu {
		border: none;
	}

	.logobox {
		height: 40px;
		line-height: 40px;
		color: #9d9d9d;
		font-size: 20px;
		text-align: center;
		padding: 20px 0px;
	}

	.logoimg {
		height: 40px;
	}
</style>

退出功能

methods: {
			exit() {
				this.$router.push({path:'/Login'})
			}
		},

TopNav.vue
概念:折叠显示图片,收缩则不显示

<template>
	<!-- <el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64"
	 text-color="#fff" active-text-color="#ffd04b">
		<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" target="_blank">订单管理</a></el-menu-item>
	</el-menu> -->
	<el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
		<el-button class="buttonimg">
			<img class="showimg" :src="collapsed?imgshow:imgsq" @click="doToggle()">
		</el-button>
		<el-submenu index="2" class="submenu">
			<template slot="title">超级管理员</template>
			<el-menu-item index="2-1">设置</el-menu-item>
			<el-menu-item index="2-2">个人中心</el-menu-item>
			<el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
		</el-submenu>
	</el-menu>
</template>

<script>
	export default {
		data() {
			return {
				//默认不折叠
				collapsed: false,
				imgshow:require('@/assets/img/show.png'),
				imgsq:require('@/assets/img/sq.png')
			}
		},
		methods: {
			exit() {
				this.$router.push({path:'/Login'})
			}
		},
	}
</script>

<style scoped>
	.el-menu-vertical-demo:not(.el-menu--collapse) {
		border: none;
	}

	.submenu {
		float: right;
	}

	.buttonimg {
		height: 60px;
		background-color: transparent;
		border: none;
	}

	.showimg {
		width: 26px;
		height: 26px;
		position: absolute;
		top: 17px;
		left: 17px;
	}

	.showimg:active {
		border: none;
	}
</style>

SPA项目开发之首页导航+左侧菜单

界面不报错,箭头样式出现向左边,功能后面才实现

点击退出可退出返回登录界面:
SPA项目开发之首页导航+左侧菜单
SPA项目开发之首页导航+左侧菜单

Vue总线的概念及使用
  • 目的:
    点击TopNav中的图片按钮,改变左侧LeftNav的样式,也就是改变该组件中的属性值,并且改变AppMain组件中的属性值
  • 分析:
    TopNav/LeftNav/AppMain这三个组件的关系
    可以看出TopNav与LeftNav是兄弟组件,他们要传值,如果通过组件通信的方式去做,是相当麻烦的,所以就出现Vue总线的概念
    SPA项目开发之首页导航+左侧菜单
    main.js
data(){
	  return {
		  Bus:new Vue({
			  
		  })
	  }
  },
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
process.env.MOCK && require('@/mock')
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import axios from '@/api/http'   
// import axios from 'axios'     
import VueAxios from 'vue-axios'

Vue.use(ElementUI)
Vue.use(VueAxios,axios)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  data(){
	  return {
		  Bus:new Vue({
			  
		  })
	  }
  },
  router,
  components: { App },
  template: '<App/>'
})

TopNav.vue

doToggle(){
				this.collapsed = !this.collapsed;
			   this.$root.Bus.$emit('collapsed-side-click',this.collapsed);
			}
<template>
	<!-- <el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64"
	 text-color="#fff" active-text-color="#ffd04b">
		<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" target="_blank">订单管理</a></el-menu-item>
	</el-menu> -->
	<el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
		<el-button class="buttonimg">
			<img class="showimg" :src="collapsed?imgshow:imgsq" @click="doToggle()">
		</el-button>
		<el-submenu index="2" class="submenu">
			<template slot="title">超级管理员</template>
			<el-menu-item index="2-1">设置</el-menu-item>
			<el-menu-item index="2-2">个人中心</el-menu-item>
			<el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
		</el-submenu>
	</el-menu>
</template>

<script>
	export default {
		data() {
			return {
				//默认不折叠
				collapsed: false,
				imgshow:require('@/assets/img/show.png'),
				imgsq:require('@/assets/img/sq.png')
			}
		},
		methods: {
			exit() {
				this.$router.push({path:'/Login'})
			},
			doToggle(){
				this.collapsed = !this.collapsed;
			   this.$root.Bus.$emit('collapsed-side-click',this.collapsed);
			}
		},
	}
</script>

<style scoped>
	.el-menu-vertical-demo:not(.el-menu--collapse) {
		border: none;
	}

	.submenu {
		float: right;
	}

	.buttonimg {
		height: 60px;
		background-color: transparent;
		border: none;
	}

	.showimg {
		width: 26px;
		height: 26px;
		position: absolute;
		top: 17px;
		left: 17px;
	}

	.showimg:active {
		border: none;
	}
</style>

LeftNav

created(){
			// v就是从Vue总线Bus获取的变量值
			this.$root.Bus.$on('collapsed-side-click',(v)=>{
				this.collapsed = v ;
			});
		}
<template>
	<el-menu default-active="2" class="el-menu-vertical-demo" background-color="#334157"
	 text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed" >
		<!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
		<div class="logobox">
			<img class="logoimg" src="../assets/img/logo.png" alt="">
		</div>
		<el-submenu index="1">
			<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 slot="title">导航三</span>
		</el-menu-item>
		<el-menu-item index="4">
			<i class="el-icon-setting"></i>
			<span slot="title">导航四</span>
		</el-menu-item>
	</el-menu>
</template>
<script>
	export default {
		data() {
			return {
				//默认不折叠
				collapsed: false
			}
		},
		created(){
			// v就是从Vue总线Bus获取的变量值
			this.$root.Bus.$on('collapsed-side-click',(v)=>{
				this.collapsed = v ;
			});
		}
	}
</script>
<style>
	.el-menu-vertical-demo:not(.el-menu--collapse) {
		width: 240px;
		min-height: 400px;
	}

	.el-menu-vertical-demo:not(.el-menu--collapse) {
		border: none;
		text-align: left;
	}

	.el-menu-item-group__title {
		padding: 0px;
	}

	.el-menu-bg {
		background-color: #1f2d3d !important;
	}

	.el-menu {
		border: none;
	}

	.logobox {
		height: 40px;
		line-height: 40px;
		color: #9d9d9d;
		font-size: 20px;
		text-align: center;
		padding: 20px 0px;
	}

	.logoimg {
		height: 40px;
	}
</style>

测试收缩:
SPA项目开发之首页导航+左侧菜单
SPA项目开发之首页导航+左侧菜单

上一篇:Element-UI里面 Cascader 级联选择器出现的过高问题


下一篇:Qt之菜单:工具条弹出式菜单