前后端分离开发开发过程当中,经常会遇到以下几个尴尬的场景:
前后端分离之后,前端迫切需要一种机制,不再需要依赖后端接口开发,而今天的主角mockjs就可以做到这一点。
Mock.js是一个模拟数据的生成器,用来帮助前端调试开发、进行前后端的原型分离以及用来提高自动化测试效率。
众所周知Mock.js因为两个重要的特性风靡前端:
npm i mockjs -D
为了只在开发环境使用mock,而打包到生产环境时自动不使用mock,我们可以在config目录中的dev.env.js和prod.env.js做一个配置,如下:
module.exports = merge(prodEnv, {
NODE_ENV: '"development"',
MOCK: 'true'
})
module.exports = {
NODE_ENV: '"production"',
MOCK: 'false'
}
//开发环境下才会引入mockjs
process.env.MOCK && require('@/mock')
创建mock目录,mock下创建index.js以及json文件夹,在JSON文件夹中创建login-mock.js
import Mock from 'mockjs' //引入mockjs,npm已安装
import action from '@/api/action' //引入请求地址
//全局设置:设置所有ajax请求的超时时间,模拟网络传输耗时
Mock.setup({
// timeout: 400 //延时400ms请求到数据
timeout: 200 - 400 //延时200-400ms请求到数据
})
//引登陆的测试数据,并添加至mockjs
import loginInfo from '@/mock/json/login-mock.js'
let s1 = action.getFullPath('SYSTEM_USER_DOLOGIN')
Mock.mock(s1, "post", loginInfo)
// Mock.mock(s1, /post|get/i, loginInfo)
//使用mockjs的模板生成随机数据
const loginInfo = {
'code|-1-0': 0,
'message|3-10': 'msg'
}
export default loginInfo;
doSubmit: function () {
let params = {
username: this.username,
password: this.password
};
console.log(params);
let url = this.axios.urls.SYSTEM_USER_DOLOGIN;
this.axios.post(url, params).then(response => {
console.log(response);
if (response.data.code == 0) {
this.$message({
message: response.data.message,
type: 'success'
})
} else {
this.$message({
message: response.data.message,
type: 'error'
})
}
}).catch(error => {
console.log(error);
});
}
使用this.$router.push({})
<template>
<div class="login-wrap">
<el-form class="login-container">
<h1 class="title">用户注册h1>
<el-form-item label="">
<el-input type="text" v-model="username" placeholder="登录账号" autocomplete="off">el-input>
el-form-item>
<el-form-item label="">
<el-input type="password" v-model="password" placeholder="登录密码" autocomplete="off">el-input>
el-form-item>
<el-form-item>
<el-button type="primary" style="width:100%;" @click="doSubmit()">提交el-button>
el-form-item>
<el-row style="text-align: center;margin-top:-10px">
<el-link type="primary">忘记密码el-link>
<el-link type="primary" @click="gotoLogin()">用户登录el-link>
el-row>
el-form>
div>
template>
<script>
export default {
name: 'Login',
data: function () {
return {
username: "admin",
password: "123"
}
},
methods: {
doSubmit: function () {
},
gotoLogin(){
this.$router.push({path: '/'})
}
}
}
script>
<style scoped>
.login-wrap {
box-sizing: border-box;
width: 100%;
height: 100%;
padding-top: 10%;
/* background-color: #112346; */
background-repeat: no-repeat;
background-position: center right;
background-size: 100%;
}
.login-container {
border-radius: 10px;
margin: 0px auto;
width: 350px;
padding: 30px 35px 15px 35px;
background: #fff;
border: 1px solid #eaeaea;
text-align: left;
box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
margin: 0px auto 40px auto;
text-align: center;
color: #505458;
}
style>
gotoRegister(){
this.$router.push({path: '/register'})
}
import Register from '@/views/Register'
//以下内容添加到routes中
{
path: '/register',
name: 'Register',
component: Register
}
//在new Vue中添加
data(){
return{
Bus: new 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">Mainel-main>
el-container>
el-container>
template>
<script>
// 导入组件
import TopNav from '@/components/TopNav.vue'
import LeftNav from '@/components/LeftNav.vue'
// 导出模块
export default {
components: {LeftNav, TopNav},
data() {
return {
asideClass: 'main-aside'
}
},
created() {
this.$root.Bus.$on('doCollapsed', v => {
//v指的是topNav传递过来的this.collapsed
this.asideClass = v ? 'main-aside-collapsed' : 'main-aside';
});
}
};
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;
transition: width 0.6s ease;
}
.main-aside {
width: 240px !important;
height: 100%;
background-color: #334157;
margin: 0px;
transition: width 0.6s ease;
}
.main-header,.main-center {
padding: 0px;
border-left: 2px solid #333;
}
style>
<template>
<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: {
doToggle() {
this.collapsed = !this.collapsed;
this.$root.Bus.$emit('doCollapsed', this.collapsed);
},
exit(){
this.$router.push({path:'/'})
}
}
}
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>
<template>
<el-menu default-active="2" class="el-menu-vertical-demo" background-color="#334157"
text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed">
<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">选项1el-menu-item>
<el-menu-item index="1-2">选项2el-menu-item>
el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="1-3">选项3el-menu-item>
el-menu-item-group>
<el-submenu index="1-4">
<template slot="title">选项4template>
<el-menu-item index="1-4-1">选项1el-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() {
this.$root.Bus.$on('doCollapsed', v => {
//v指的是topNav传递过来的this.collapsed
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>
{
path: '/appMain',
name: 'AppMain',
component: AppMain,
children: [{
path: '/TopNav',
name: 'TopNav',
component: TopNav
},{
path: '/leftNav',
name: 'LeftNav',
component: LeftNav
}
]
}
4.登出
//在methods中添加exit()方法
exit(){
this.$router.push({path:'/login'})
}