zoukankan      html  css  js  c++  java
  • vueelementadmin重定向分析

    1.登录页面的重定向分析

    login.vue 中对 $route 进行监听:

    watch: {
      $route: {
        handler: function(route) {
          const query = route.query
          if (query) {
            this.redirect = query.redirect
            this.otherQuery = this.getOtherQuery(query)
          }
        },
        immediate: true
      }
    }

    this.getOtherQuery(query) 的用途是获取除 redirect 外的其他查询条件,登录成功后:

    this.$store.dispatch('user/login', this.loginForm)
    .then(() => {
      this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
      this.loading = false
    })
    .catch(() => {
      this.loading = false
    })

    完成重定向的代码为:

    this.$router.push({ path: this.redirect || '/', query: this.otherQuery })

    2.重定向组件

    vue-element-admin 提供了专门的重定向组件,源码如下:

    <script>
    export default {
      created() {
        const { params, query } = this.$route
        const { path } = params
        this.$router.replace({ path: '/' + path, query })
      },
      render: function(h) {
        return h() // avoid warning message
      }
    }
    </script>

    重定向组件配置了动态路由:

    {
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [
          {
            path: '/redirect/:path*',
            component: () => import('@/views/redirect/index')
          }
        ]
    }

    这里有一个细节:

    path: '/redirect/:path*'

    表示匹配零个或多个路由,比如路由为 /redirect 时,仍然能匹配到 redirect 组件。如果将路由改为:

    path: '/redirect/:path'

    此时路由 /redirect 将只能匹配到 Layout 组件,而无法匹配到 redirect 组件

  • 相关阅读:
    小工具
    git
    git如何做个人构建
    菜鸟教程
    Xftp和Xshell
    IDEA
    webStorm
    HBuilder
    chrome浏览器
    Vue-Server-Renderer
  • 原文地址:https://www.cnblogs.com/pwindy/p/15562338.html
Copyright © 2011-2022 走看看