(一)axios 封装

(1)axios拦截器

可以在axios中加入加载的代码。。。

(2)封装请求

后期每个请求接口都可以写在这个里面。。。

(二)vuex

user.js

import { login } from '@/api/login'

const state = {
userInfo: null,
} const getters = {
get_userInfo (state) {
let userInfo = state.userInfo
if (!userInfo) {
userInfo = window.localStorage.getItem('userInfo')
}
return JSON.parse(userInfo)
}
} const mutations = {
set_userInfo (state, data) {
state.userInfo = data
window.localStorage.setItem('userInfo', state.userInfo)
}
} const actions = {
Login (context, {username, password}) {
return new Promise((resolve, reject) => {
login(username, password).then(response => {
context.commit('set_userInfo', response.data.rtnInfo)
resolve(response)
}, error => {
reject(error)
})
})
}
} export default {
state,
getters,
mutations,
actions
}

(1)创建一个state - userInfo  保存用户信息

(2)getters - get_userInfo 获取用户信息,读取缓存

(3)mutations - set_userInfo  每次登录,将用户信息保存在缓存中

(4)action - Login 调用api中的login接口

进行登录操作

在index.js 中注入 user

在main.js中 引入store - index.js

(三)组件中运用

Login.Vue

<template>
<div id="login">
<img class="login-icon01" src="../../static/images/login02.png">
<el-form class="login-form" :model="ruleForm" ref="ruleForm">
<el-form-item>
<el-input type="text" placeholder="用户名" v-model="ruleForm.username" prefix-icon="iconfont icon-user" clearable auto-complete="off"></el-input>
</el-form-item>
<el-form-item>
<el-input type="password" placeholder="密码" v-model="ruleForm.password" prefix-icon="iconfont icon-password" clearable auto-complete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button class="login-btn" type="primary" :loading="loading" @click="submitForm(ruleForm)">登录</el-button>
</el-form-item>
</el-form>
<img class="login-icon03" src="../../static/images/login01.png">
</div>
</template> <script>
export default {
data () {
return {
loading: false,
// urlIbest: this.$store.state.User.urlIbest,
ruleForm: {
username: '',
password: ''
}
}
},
methods: {
submitForm (formName) {
this.loading = true
if (formName.username === '' || formName.password === '') {
this.$message.error('用户名或密码不能为空')
this.loading = false
} else {
// 登录
this.$store.dispatch('Login', {'username': formName.username, 'password': formName.password}).then(response => {
if (response.data && response.data.rtnCode === '00000000') {
this.loading = false
this.$router.push('/home')
} else {
this.$message.error(response.data.rtnMsg)
this.loading = false
}
})
}
}
}
}
</script> <style lang="less" scoped>
#login {
background-color: #2f329f;
position: fixed;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
.login-form{
width: 80%;
padding: 30px 10%;
background: rgba(47,50,159,.3);
position: absolute;
z-index: 8;
top: 120px;
.el-input__inner{
padding-top:8px;
padding-bottom:8px;
background-color:transparent!important;
color:#fff;
}
.login-btn{
width:100%;
background-color:#fff;
color:#2f329f;
border:none;
margin-top:20px;
}
}
.login-icon01{
position: absolute;
width: 20%;
right: 15%;
top: 60px;
z-index: 10;
}
.login-icon02{
position: absolute;
width: 50%;
bottom:20px;
left:0;
right:0;
margin:auto;
z-index:2;
}
.login-icon03{
position: absolute;
width: 110%;
left: 0;
right: 0;
bottom: 0;
margin-left: -5%;
z-index: 1;
}
}
</style>

在登录提交中,调用store中方法

this.$store.dispatch('Login', {'username': formName.username, 'password': formName.password})

传入用户名和密码

Vue(二十三)vuex + axios + 缓存 运用 (以登陆功能为例)的更多相关文章

  1. Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...

  2. 基于 vue+vue-router+vuex+axios+koa+koa-router 本地开发全栈项目

    因为毕业设计要做基于Node服务器的项目,所以我就想着用刚学的vue作为前端开发框架,vue作为Vue.js应用程序的状态管理模式+库,axios基于promise用于浏览器和node.js的http ...

  3. Vue项目中使用Vuex + axios发送请求

    本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...

  4. vue+vuex+axios+echarts画一个动态更新的中国地图

    一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china- ...

  5. Vue 爬坑之路—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios  ...

  6. day 84 Vue学习六之axios、vuex、脚手架中组件传值

    Vue学习六之axios.vuex.脚手架中组件传值   本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的 ...

  7. vue全家桶项目搭建(vue-cli 2.9.6+vue-router+vuex+axios)

    一.安装vue-cli + vue-router + vuex + axios 1.安装vue-cli 2.创建项目 3.安装vuex和axios 二.搭建项目目录结构,如下所示: 1.assets目 ...

  8. 使用Typescript重构axios(二十三)——添加withCredentials属性

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  9. Vuex+axios

    Vuex+axios   Vuex简介 vuex是一个专门为Vue.js设计的集中式状态管理架构. 状态? 我们把它理解为在data中需要共享给其他组件使用的部分. Vuex和单纯的全局对象有以下不同 ...

随机推荐

  1. The connection string 'MysqlEF' in the application's configuration file does not contain the require异常

    在学习EF core first 对接mysql时,出现了这个异常. 原因是:连接字符串中缺少providerName="MySql.Data.MySqlClient" <a ...

  2. mysql四大特性与四种隔离级别

    四大特性 1:原子性.事务是一个不可分割的整体,事务开始的操作,要么全部执行,要么全部不执行. 2:隔离性.同一时间,只允许一个事务请求同一组数据.不同的事务彼此之间没有干扰. 3:一致性.事务开始前 ...

  3. python全栈开发day70-Django中间件

    参考个人博客 http://wuchengyi.com/post/13/ 三.预习和扩展

  4. Kudu Native RDD

    Spark与Kudu的集成同事提供了kudu RDD import org.apache.kudu.spark.kudu.KuduContext import org.apache.spark.{Sp ...

  5. excel追加数据

    原本是想通过读取已存在的文件的行然后直接添加保存,发现结果会被覆盖. 后来查找方法发现需要复制原表. 函数参数: list:要添加的数据 filename:目标文件 sheet_index:默认表的第 ...

  6. mysql-索引-日志

    索引:基于元数据之上的在某个字段或多个字段取出来,索引是加速读操作的,但对写操作时有副作用的 BTree 索引:抽取出来重新排序,是左前缀索引每一个叶子结点到根结点的距离相同: 哈希索引:基于键查找值 ...

  7. Mysql my.cnf配置文件记录

    一.参数 1.max_binlog_size = 1G      #binlog大小 2. #slave不需要同步数据库 binlog-ignore-db=information_schema bin ...

  8. P1010 幂次方 递归模拟

    题目描述 任何一个正整数都可以用22的幂次方表示.例如 137=2^7+2^3+2^0137=27+23+20 同时约定方次用括号来表示,即a^bab 可表示为a(b)a(b). 由此可知,13713 ...

  9. P2158 [SDOI2008]仪仗队

    P2158 [SDOI2008]仪仗队图是关于y=x对称的,横纵坐标一定是互质的否则在之前就被扫过了,所以就可以用欧拉函数再*2就完了. #include<iostream> #inclu ...

  10. PostgreSQL 调用存储过程返回结果集

    创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...