• 增加退出功能


    1). 添加退出功能

    打开 src/store/index.js 文件,在 actions 添加退出事件 logout

    src/store/index.js

     1 const actions = {
     2   login({ commit }, user) {
     3     if (user) commit('UPDATE_USER', user)
     4     commit('UPDATE_AUTH', true)
     5     router.push('/')
     6   },
     7   logout({ commit }) {
     8     commit('UPDATE_AUTH', false)
     9     router.push({ name: 'Home', params: { logout: true } })
    10   }
    11 }

    退出的时候,我们将 auth 状态更改为未登录,并跳转到首页。在路由 push 方法的第一个参数使用对象,可以添加更多的配置。我们这里指定跳转目标的名称为 Home,对应 src/router/index.js 下 name 为 Home的路由。同时,我们传了一个 logout 参数,当首页获取到该参数时,就显示一个操作成功的提示。

     

    2). 绑定退出事件

    1、打开 src/components/layouts/TheEntry.vue 文件,添加退出方法 logout(注释部分是涉及的修改):

    src/components/layouts/TheEntry.vue

     1 <script>
     2 import { mapState } from 'vuex'
     3 
     4 export default {
     5   name: 'TheEntry',
     6   computed: {
     7     ...mapState([
     8       'auth',
     9       'user'
    10     ])
    11   },
    12   // 添加 methods 选项,并添加 logout 方法
    13   methods: {
    14     logout() {
    15       this.$store.dispatch('logout')
    16     }
    17   }
    18 }
    19 </script>

    点击退出后,我们通过调用 this.$store.dispatch('logout') 来完成退出。

    2、查找 <ul class="dropdown-menu">,在退出链接上添加 @click="logout"

    src/components/layouts/TheEntry.vue

    1 <ul class="dropdown-menu">
    2   <li><a href="javascript:;" @click="logout"><i class="fa fa-sign-out text-md"></i>退出</a></li>
    3 </ul>

    3). 添加退出成功提示

    打开 src/views/Home.vue 文件,复制以下代码替换原 <script>

    src/views/Home.vue

     1 <script>
     2 export default {
     3   name: 'Home',
     4   data() {
     5     return {
     6       msg: '', // 消息
     7       msgType: '', // 消息类型
     8       msgShow: false // 是否显示消息,默认不显示
     9     }
    10   },
    11   beforeRouteEnter(to, from, next) {
    12     const fromName = from.name
    13     const logout = to.params.logout
    14 
    15     next(vm => {
    16       if (vm.$store.state.auth) {
    17         switch (fromName) {
    18           case 'Register':
    19             vm.showMsg('注册成功')
    20             break
    21         }
    22       } else if (logout) {
    23         vm.showMsg('操作成功')
    24       }
    25     })
    26   },
    27   computed: {
    28     auth() {
    29       return this.$store.state.auth
    30     }
    31   },
    32   watch: {
    33     auth(value) {
    34       if (!value) {
    35         this.showMsg('操作成功')
    36       }
    37     }
    38   },
    39   methods: {
    40     showMsg(msg, type = 'success') {
    41       this.msg = msg
    42       this.msgType = type
    43       this.msgShow = true
    44     }
    45   }
    46 }
    47 </script>

    我们这里需要处理退出的两种情况。

    从其他页面退出时,我们通过路由参数 logout 来判断是否显示提示:

     1 beforeRouteEnter(to, from, next) {
     2   const fromName = from.name
     3   // 获取 logout 参数
     4   const logout = to.params.logout
     5 
     6   next(vm => {
     7     if (vm.$store.state.auth) {
     8       switch (fromName) {
     9         case 'Register':
    10           vm.showMsg('注册成功')
    11           break
    12       }
    13     } else if (logout) {
    14       // logout 返回 true 时,显示操作成功提示
    15       vm.showMsg('操作成功')
    16     }
    17   })
    18 }

    从首页退出时,路由未改变,beforeRouteEnter 不会被调用,我们通过侦听 auth 来判断是否显示提示:

     1 computed: {
     2   // 用户登录状态
     3   auth() {
     4     return this.$store.state.auth
     5   }
     6 },
     7 watch: {
     8   // 监听 auth,它的值变为 false 时,显示操作成功提示
     9   auth(value) {
    10     if (!value) {
    11       this.showMsg('操作成功')
    12     }
    13   }
    14 },
  • 相关阅读:
    Minutes和TotalMinutes的区别
    C#的"?"修饰符和"??"运算符
    Navicat 连接MySQL 8.0.11 出现2059错误
    EL1004E: Method call: Method fmtdate(java.util.Date,java.lang.String) cannot be found on org.thymele
    es nested结构判断不为空
    es nested嵌套查询
    CPU基础知识线程切换
    CPU基础知识CPU的组成 运算器、控制器、寄存器
    几个常用寄存器
    Linux笔记用户态与内核态
  • 原文地址:https://www.cnblogs.com/yangguoe/p/9309993.html
Copyright © 2020-2023  润新知