流浪のwolf

卷帝

导航

< 2025年3月 >
23 24 25 26 27 28 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 26 27 28 29
30 31 1 2 3 4 5

统计

使用 vue2 + element-ui 登录的时候的逻辑

1. 自动校验表单逻辑

复制代码
  // 1. 自动表单验证
      try {
        // 这个形式自动表单验证麻烦
        // this.$refs.loginForm.validate((valid)=>{ ... });
        // 返回promise,如果失败则出错,程序不往下走
        // await this.$refs.loginForm.validate();
      } catch (error) {
        return console.log(error);
      }
复制代码

 

2. 派发dispatch vuex 的  actions 执行异步请求获取token

3. 提交comutations 来保存 token 

vuex 全局保存 token 借助 js-cookies 来做 token 的持久化 ;

可以节流按钮 loding 

 

复制代码
    async handleLogin() {
      // 1. 自动表单验证
      try {
        // 这个形式自动表单验证麻烦
        // this.$refs.loginForm.validate((valid)=>{ ... });
        // 返回promise,如果失败则出错,程序不往下走
        await this.$refs.loginForm.validate();
      } catch (error) {
        return console.log(error);
      }
      // 登录中
      /*******
       * 节流阀
       * 当返回数据的时候才会loading 为 true 登录按钮才可以被使用 否则一直处于加载状态
       */
      this.loading = true;
      // 派发获取 token 值
      try {
        // 2.发生登录请求
        await this.$store.dispatch("user/loginAction", this.loginForm);
        // 3.成功跳转页面
        this.$router.push("/");
      } catch (error) {
        console.log("登录失败", error);
      }
      // 登录结束
      this.loading = false;
    },
复制代码

 

posted on   朱龙旭的网络  阅读(77)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示