广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue关闭当前页、关闭当前标签tagsView的实现方法
  • 629
分享到

Vue关闭当前页、关闭当前标签tagsView的实现方法

Vue关闭当前页Vue关闭当前标签tagsView 2022-11-13 14:11:45 629人浏览 泡泡鱼
摘要

由于项目使用tagsView,关闭当前页面需要通过关闭当前标签来实现  涉及到几个点: 1. 移除 VisitedView 和 CachedView 中的当前项

由于项目使用tagsView,关闭当前页面需要通过关闭当前标签来实现 

涉及到几个点:

1. 移除 VisitedView 和 CachedView 中的当前项

2. 跳转到最后一次访问的标签

主要思路:比对 路由路径 ( this.$route.path)

两种方式:

一、 在Vue页面直接实现

closePage()
      var currentView = this.$store.state.tagsView.visitedViews[0]
      for (currentView of this.$store.state.tagsView.visitedViews) {
        if (currentView.path === this.$route.path) {
          break
        }
      }
      this.$store.dispatch('tagsView/delView', currentView)
        .then(({ visitedViews }) => {
          if (currentView.path === this.$route.path) {
            const latestView = this.$store.state.tagsView.visitedViews.slice(-1)[0]
            if (latestView) {
              this.$router.push(latestView)
            } else {
              // 如果没有其他标签则跳转到首页
              if (currentView.name === '首页') {
                this.$router.replace({ path: '/redirect' + currentView.fullPath })
              } else {
                this.$router.push('/')
              }
            }
          }
        })

二、在js文件中写自定义函数,在vue页面中调用

import router from '@/router/routers'
 
// 关闭当前页 关联tagView
export function closePage(store, route) {
  var currentView = store.state.tagsView.visitedViews[0]
  for (currentView of store.state.tagsView.visitedViews) {
    if (currentView.path === route.path) {
      break
    }
  }
  store.dispatch('tagsView/delView', currentView)
    .then(({ visitedViews }) => {
      if (currentView.path === route.path) {
        const latestView = store.state.tagsView.visitedViews.slice(-1)[0]
        if (latestView) {
          router.push(latestView)
        } else {
          if (currentView.name === '首页') {
            router.replace({ path: '/redirect' + currentView.fullPath })
          } else {
            router.push('/')
          }
        }
      }
    })
}

到此这篇关于Vue 关闭当前页、关闭当前标签tagsView的文章就介绍到这了,更多相关Vue 关闭当前页内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue关闭当前页、关闭当前标签tagsView的实现方法

本文链接: https://www.lsjlt.com/news/172196.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作