登录流程

添加Axios :发请求

import Axios from 'axios'
Vue.prototype.$ajax  = Axios;

因为产生跨域,所以配置代理

baseUrl?

// baseUrl
Axios.defaults.baseURL = '/api'

vue.config.js
在这里插入图片描述

module.exports = {
    configureWebpack: {
       devServer:{
        proxy:{
            '/api':{
                target:'http://1.116.64.64:5004/',
                changeOrigin:true
            }}}}}

按钮的点击事件,添加登录函数

  submitForm(formName) {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.doLogin(this.ruleForm)
                } else {
                    console.log('error submit!!');
                    return false;
                } });},

登录方法

 //登录方法
        async doLogin({username,pass}) {
            try {
                var res = await this.$ajax.post('/user/login', {
                    account: username,
                    password: pass
                });
                console.log(res);
            } catch (e) {
                console.log(e, 'err')} },

在这里插入图片描述
产生错误是因为el-form没有加:model=“ruleForm”,:model 是element-ui里面的属性和v-model不一样。
在这里插入图片描述
post 报错,404
原因是把代理文件放在src里面了,拿出来就好了
在这里插入图片描述
请求发送成功
在这里插入图片描述
在这里插入图片描述
得到token

使用axios实例发送请求

 let instance = Axios.create({
                    baseURL:'/api'
                })
                instance.post("/user/login",{
                    account:username,
                    password:pass
                })
                .then(res => {
                    console.log(res);
                })
                .catch(w => {
                    console.log(e)
                });
        
            } catch (e) {
                console.log(e, 'err')
            }

post请求

登录成功后改变用户状态和用户信息

  					// 改变用户信息
                    if(res.data.code === 20000) {
                        this.changeLogin(true)
                        // 改变用户信息
                        this.changeUserInfo({
                            username:username
                        });
                        this.$router.push('/')
                    } else {
                        console.log(res)
                        alert('登录失败')
                    }

在这里插入图片描述
跳转成功

Created with Raphaël 2.3.0 导入axios 解决跨域,配置代理 axios发送请求,获取token 判断登录成功,改变用户状态和信息 跳转成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值