Vue(二十三)vuex + axios + 缓存 运用 (以登陆功能为例)

(一)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})

传入用户名和密码

原文地址:https://www.cnblogs.com/yulingjia/p/8986387.html

时间: 2024-08-03 04:36:15

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

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

一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china-map # 安装项目依赖 npm install # 安装 vuex npm install vuex --save # 安装 axios npm install axios --save # 安装 ECharts npm install echarts --save 二. 项目结构 ├── ind

Vuex + axios 发送请求

Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios 来完成 ajax 请求,而大型项目都会使用 Vuex 来管理数据,所以这篇博客将结合两者来发送请求 前言: Vuex 的安装将不再赘述,可以参考之前的博客 Vue 爬坑之路(四)-- 与 Vuex 的第一次接触 使用 cnpm 安装 axios cnpm install axios -S 安装其他插

Vue状态管理vuex

转: https://www.cnblogs.com/xiaohuochai/p/7554127.html 前面的话 由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue提供了vuex.本文将详细介绍Vue状态管理vuex 引入 当访问数据对象时,一个 Vue 实例只是简单的代理访问.所以,如果有一处需要被多个实例间共享的状态,可以简单地通过维护一份数据来实现共享 const sourceOfTruth = {} const vmA = new

angular学习笔记(二十三)-$http(1)-api

之前说到的$http.get和$http.post,都是基于$http的快捷方式.下面来说说完整的$http: $http(config) $http接受一个json格式的参数config: config的格式如下: { method:字符串 , url:字符串, params:json对象, data:请求数据, headers:请求头, transformRequest:函数,转换post请求的数据的格式, transformResponse:函数,转换响应到的数据的格式, cache:布尔

云计算设计模式(二十三)——Throttling节流模式

云计算设计模式(二十三)——Throttling节流模式 控制由应用程序使用,一个单独的租户或整个服务的一个实例的资源的消耗.这种模式可以允许系统继续运行并满足服务水平协议,即使当增加需求的资源放置一个极端载荷. 背景和问题 在云应用负载通常上变化的基础上的活动用户的数量或他们正在执行的活动类型的时间.例如,多个用户可能会在工作时间被激活,否则系统可能被要求在每月结束时执行计算昂贵的分析.也有可能是突然和意外的突发活动.如果系统的处理要求超过了可用的资源的能力,其将遭受性能不佳,甚至会失败.该系

微信小程序把玩(二十三)modal组件

原文:微信小程序把玩(二十三)modal组件 modal弹出框常用在提示一些信息比如:退出应用,清楚缓存,修改资料提交时一些提示等等. 常用属性: wxml <!--监听button点击事件--> <button bindtap="listenerButton" type="primary">弹出modal</button> <!--弹出框--> <modal title="退出应用" hid

Weex/Vue项目中静态资源缓存处理.md

目录 一.项目缓存问题 二.浏览器本地缓存 三.解决方案 今年公司技术考察,考察了EMAS平台,从而接触了weex,并且为了进行POC测试,大胆采用weex进行新web项目试点.weex内置了vue框架,整体框架与vue一致,好在刚接触了vue一段时间,因此用起来还算顺手,碰到问题weex官方文档没有的,基本都可以参考vue的文档. 一.项目缓存问题 第一次接触这类前后端完全分离的开发模式,一开始的确是一头雾水,一个多月时间,难点一点一点克服,目前进入项目测试阶段,经常碰到bug修复了,测试人员

vue2.0+webpack+vuerouter+vuex+axios构建项目基础

前言 本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础 步骤 1.全局安装webpack,命令 npm install webpack -g 注意,webpack升级到4舍弃了不少组件,之前有次使用淘宝镜像丢失了不少模块,所以webpack大家尽量使用npm装. 2.安装vue脚手架 npm install vue-cli -g 3.运行cmd(开始-运行-cmd-回车) 比如你的目录要安装在E盘,在命令面板中就输入"e:"然后回车cd到项

winform学习日志(二十三)---------------socket(TCP)发送文件

一:由于在上一个随笔的基础之上拓展的所以直接上代码,客户端: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using Sys