VUE路由跳转并刷新页面(框架层实现)

前言

        网上找了很多办法,都需要开发者在页面内单独实现,或者是刷新整个浏览器,感觉并不是特别舒服。因此,我考虑可以在框架层去实现路由跳转刷新。

        思路如下:

                1、重定向至临时界面(用户无感知)

                2、打开临时界面时,由于触发了TagsView的watch路由事件,判断是重定向请求界面,于是关闭已经渲染的目标界面

                3、进入临时界面后,再跳回目标界面。这时候就可以重新打开新的界面了

步骤1:配置路由信息

// 动态路由:通过匹配name、path进行重定向的界面
for (const menu of [动态菜单权限]) {
    [动态路由数组].push({
              path: '/redirect' + menu.uri,
              component: () => import('@/views/frame/redirect/index'),
              name: 'redirect/' + diyRoutes[menu.uri].name,
              hidden: true
            })
}

// 固定路由:通过正则匹配所有重定向请求
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/frame/redirect/index')
      }
    ]
  }
}

步骤2:创建重定向空白页

文件路径:@/views/frame/redirect/index.vue

<script>
export default {
  created() {
    if (this.$route.name && this.$route.name.startsWith('redirect/')) {
      const path = this.$route.path.substring(9)
      this.$router.replace({ path: path, params: this.$route.params, query: this.$route.query })
    } else {
      this.$router.replace({ path: '/' + this.$route.params.path, params: this.$route.params, query: this.$route.query })
    }
  },
  render: function(h) {
    return h() // avoid warning messageN
  }
}
</script>

步骤3:TagsView/index.vue 关闭目标界面

watch: {
    $route() {
      // 如果是重定向到界面,需要重新打开渲染界面
      if (this.$route.path.startsWith('/redirect/')) {
        const path = this.$route.path.substring(9)
        // 获取route对象
        for (const route of this.$store.state.tagsView.visitedViews) {
          if (route.path === path) {
            this.closeSelectedTag(route)
            break
          }
        }
      } else {
        this.addTags()
        this.moveToCurrentTag()
      }
    },
}

步骤4:跳转代码

// 通过name跳转
this.$router.push({ name: 'redirect/user' })

// 通过path跳转
this.$router.push({ path: '/redirect/user/index' })

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue中,当我们需要进行页面跳转刷新的时候,可以采用以下解决办法。首先,在需要页面刷新的地方,我们可以使用`this.$router.push('/emptyPage')`来跳转到一个空白页。然后,在空白页的`emptyPage.vue`文件中,我们可以使用`beforeRouteEnter`钩子来控制页面跳转,从而实现刷新的效果。具体的代码如下: ```javascript // 在需要页面刷新的地方 this.$router.push('/emptyPage'); // 在emptyPage.vue文件中 beforeRouteEnter(to, from, next) { next(vm => { vm.$router.replace(from.path); }); } ``` 这样,当我们进行页面跳转时,会先跳转到空白页,然后在空白页的`beforeRouteEnter`钩子中,通过`vm.$router.replace(from.path)`将页面重新跳转回原来的路径,从而实现页面的刷新效果。这种方法可以避免整个浏览器进行刷新加载,减少页面闪烁,提升用户体验。另外,我们还可以使用`name`或`path`来进行跳转,具体的代码如下: ```javascript // 通过name跳转 this.$router.push({ name: '/redirect/user' }); // 通过path跳转 this.$router.push({ name: '/redirect/user/index' }); ``` 通过以上方法,我们可以在Vue实现路由跳转刷新页面的需求。 #### 引用[.reference_title] - *1* [vue router跳转页面刷新 跳转后的页面](https://blog.csdn.net/qq_21190847/article/details/107830211)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue关于页面刷新的几个方式](https://blog.csdn.net/weixin_45990864/article/details/123147531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [VUE路由跳转刷新页面框架实现)](https://blog.csdn.net/qq_18984887/article/details/128300457)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值