vue项目实战(一)——资金管理系统-前端02

14 篇文章 0 订阅

1.安装element-ui
在前端项目中用到的ui 是饿了么公司的Vue-UI。安装如下:

cnpm i element-ui -S

在这里插入图片描述
2.新建client\src\views\Register.vue

<template>
  <div class="register">
    <selection class="form_container">
      <div class="manage_tip">
        <span class="title">米修在线后台管理系统</span>
      </div>
    </selection>
  </div>
</template>

<script>
export default {
  name: "register",
  components: {}
};
</script>
<style scoped>
.register {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(../assets/bg.jpg) no-repeat center center;
  background-size: 100% 100%;
}
.form_container {
  width: 370px;
  height: 210px;
  position: absolute;
  top: 10%;
  left: 34%;
  padding: 25px;
  border-radius: 5px;
  text-align: center;
}
.form_container .manage_tip .title {
  font-family: "Microsoft YaHei";
  font-weight: bold;
  font-size: 26px;
  color: #fff;
}
.registerForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}

.submit_btn {
  width: 100%;
}
</style>

3.在client\src\router.js添加对Register.vue的引用:

import Register from './views/Register.vue'
	//...
export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [        
        {
            path: '/register',
            name: 'register',
            component: Register
        },
    ]
})	

4.如何在vuecli3.0实现跨域请求
client目录下新建http.js文件:

import axios from 'axios';
import {
    Message,
    Loading
} from 'element-ui';
import router from './router';

let loading; //定义loading变量

function startLoading() { //使用Element loading-start 方法
    loading = Loading.service({
        lock: true,
        text: '拼命加载中',
        background: 'rgba(0, 0, 0, 0.7)'
    })
}

function endLoading() { //使用Element loading-close 方法
    loading.close()
}

// 请求拦截
axios.interceptors.request.use(
    config => {
        //加载动画
        startLoading();
        return config;
    },
    eror => {
        return Promise.reject(error);
    }
)


//响应拦截
axios.interceptors.response.use(
    response => {
        //结束加载动画
        endLoading();
        return response;
    },
    error => {
        //错误提醒
        endLoading();
        Message.error(error.response.data);
        return Promise.reject(error);
    }
)
export default axios;

5.实现路由守卫
client\src\route.js中加入以下代码:

// 添加路由守卫
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.eleToken ? true : false;
    if (to.path == "/login" || to.path == "/register") {
        next();
    } else {
        isLogin ? next() : next("/login");
    }
});

export default router;

6.过期token处理:
client\src\http.js中添加代码:

// 请求拦截
axios.interceptors.request.use(
    config => {
        //加载动画
        startLoading();
        if (localStorage.eleToken) {
            config.headers.Authorization = localStorage.eleToken;
        }
        return config;
    },
    eror => {
        return Promise.reject(error);
    }
)


//响应拦截
axios.interceptors.response.use(
    response => {
        //结束加载动画
        endLoading();
        return response;
    },
    error => {
        //错误提醒
        endLoading();
        Message.error(error.response.data);
        // 获取错误状态码
        const {
            status
        } = error.response;
        if (status == 401) {
            Message.error('token值无效,请重新登录');
            // 清除token
            localStorage.removeItem('eleToken')
            //页面跳转
            router.push('/login')
        }
        return Promise.reject(error);
    }
)

7.使用jwt-decode模块来解析token.

cnpm install jwt-decode
// 解析token
const decoded = jwt_decode(token);

8.把token存储到vuex:
store.js中加入:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const types = {
    SET_IS_AUTHENTICATED: 'SET_IS_AUTHENTICATED', //是否通过认证
    SET_USER: 'SET_USER' //用户信息
}
const state = { //需要维护的状态
    isAuthenticated: false,
    user: {}

}
const getters = {
    isAuthenticated: state => state.isAuthenticated,
    user: state => state.user
}
const mutations = {
    [types.SET_IS_AUTHENTICATED](state, isAuthenticated) {
        if (isAuthenticated)
            state.isAuthenticated = isAuthenticated
        else
            state.isAuthenticated = false
    },
    [types.SET_USER](state, user) {
        if (user)
            state.user = user
        else
            state.user = {}
    }
}
const actions = {
    setIsAuthenticated: ({
        commit
    }, isAuthenticated) => {
        commit(types.SET_IS_AUTHENTICATED, isAuthenticated)
    },
    setUser: ({
        commit
    }, user) => {
        commit(types.SET_USER, user)
    },
    clearCurrentState: ({
        commit
    }) => {
        commit(types.SET_IS_AUTHENTICATED, false)
        commit(types.SET_USER, null)
    }

}

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
})

client\src\view\Login.vue中添加:

//存储数据
            this.$store.dispatch("setIsAuthenticated", !this.isEmpty(decoded));
            this.$store.dispatch("setUser", decoded);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# URL: http://kongkong.fence.wmdev2.lsh123.com/ # jl-mis(接龙后台管理系统) 项目是基于vue-cli 3+ 构建的,请先将vue-cli升级到3+版本,vue-cli3 传送门(https://cli.vuejs.org/zh/)。 ## 技术栈 vue2 + vuex + vue-router + element-ui + webpack + ES6/7 + axios + less + flex + svg ### 项目图形化管理界面 ``` vue ui ``` ## 项目运行 #### 注意:由于涉及大量的 ES6/7 等新属性,node 需要 8.0 以上版本 ``` npm install npm run serve ``` ### 打包 ``` npm run build ``` ### 代码的 lint ``` npm run lint ``` # 项目布局 ``` . ├── public // HTML 和静态资源 │   ├── favicon.ico // 图标 │   ├── index.html // 入口html文件 ├── src // 源码目录 │   ├── assets // 静态资源 │   │   ├── images // 公共图片 │   ├── components // 组件 │   │   ├── common // 公共组件 │   │   ├── page // 页面 │   ├── libs // 封装库 │   │   ├── storage.js // 对cookie 和 localStorage 的封装 │   ├── plugins // 引用的插件 │   │   ├── axios.js // 对axios的的封装(拦截器相关) │   │   ├── element.js // 引入element-ui │   ├── router │   │   └── router.js // 路由配置 │   ├── service // 数据交互统一调配 │   │   ├── service.js // 获取数据的统一调配文件,对接口进行统一管理 │   ├── store // vuex的状态管理 │   │   ├── actions.js // 配置actions │   │   ├── getters.js // 配置getters │   │   ├── store.js // 引用vuex,创建store │   │   ├── modules // store模块 │   │   │   ├── urlGroups.js // 路由分组(权限相关) │   │   ├── mutation-types.js // 定义常量muations名 │   │   └── mutations.js // 配置mutations │   └── style // 样式字体相关 │   ├── fonts // 字体相关 │   ├── utility.less // 公共样式文件 │   ├── mixin.less // 样式配置文件 │   └── normalize.css // 样式重置 │   ├── App.vue // 页面入口文件 │   ├── main.js // 程序入口文件,加载各种公共组件 ├── vue.config.js // vue-cli 3+ 暴露出来的webpack相关的配置文件 . ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值