码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • mock+封装axios+vuex分发


    封装axios

    import axios from "axios";
    
    const requests = axios.create({
    
        // 基础路径
        baseURL:"/mock",
        // 超时时间
        timeout:5000,
    });
    
    //请求拦截器
    requests.interceptors.request.use((config)=>{
        // config:配置对象,对象里有一个重要属性,header请求头
        return config;
    })
    
    //相应拦截器
    requests.interceptors.response.use((res)=>{
        return res.data
    },(error)=>{
        return Promise.reject(new Error('failed'))
    })
    
    //对外暴露
    export default requests;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    编写对应接口并对外暴露

    import mockRequests from './request'
    
    // 获取bannerList的接口
    export const reqGetBannerList = ()=>mockRequests.get(`/banner`);
    
    // 注册接口
    export const registerResponse = (params)=>mockRequests.post(`/register`,params)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    编写返回结果

    import Mock from 'mockjs'
    
    // banner是json数据需自己新建json文件填入
    import banner from './banner.json'
    
    Mock.mock("/mock/banner",{code:200,data:banner});
    
    Mock.mock("/mock/register",'post',function(options){
        // options里有url type body
        let o = JSON.parse(options.body)
        if(o.username === 'xiaohua'){
            return{
                code:212,
                message:"用户名重复"
            }
        }else{
            return {
                code:200,
                message:"注册成功"
            }
        }
        
    })
    
    options {url: '/mock/register', type: 'POST', body: '{"username":"xiaohua","password":"123"}'}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    注册:
    点击注册按钮,在click事件中分发去注册,再action中发送异步请求,请求registerResponse接口

        methods: {
          postRegister(){
            let username = this.userName;
            let pw = this.passWord;
            // console.log("name:",username,"pw:",pw)
            if(username!="" && pw!=""){
              this.$store.dispatch('goRegister',{
                username:this.userName,password:this.passWord,
              });
            }
          }
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
      actions:{
        async goRegister(context,param){
            let result = await registerResponse(param);
            console.log(result);
            // 注册成功则跳转登录页
            if(result.code == 200){
              router.push({path:'/login'})
              window.alert(result.message)
            }else{
            // 注册失败返回失败原因
              alert(result.message)
            }
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    流媒体分析之srt 之libsrt 分析:
    彻底解决tomcat控制台提示至少有一个JAR被扫描用于TLD但尚未包含TLD的问题
    精通Java事务编程(6)-可串行化隔离级别之真串行
    CMT2380F32模块开发8-Base Timer例程
    微软发布Phi-3,手机上就能跑,是时候聊聊小型语言模型了|TodayAI
    工程数学笔记 | 傅里叶级数/变换的本质理解
    SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动)
    敏感词在线检测-敏感词在线检测工具
    软件工程毕业设计课题(11)基于python的毕业设计python校园二手交易系统毕设作品源码
    深度学习【PyTorch中序列化容器、QA机器人、环境准备】
  • 原文地址:https://blog.csdn.net/Paranoia_yy/article/details/126199428
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号