美文网首页
vue 解决浏览器后退路由变化,父路由状态不变问题

vue 解决浏览器后退路由变化,父路由状态不变问题

作者: 醉笙情丶浮生梦 | 来源:发表于2019-04-24 17:41 被阅读0次
image

中间的是子路由 跳转到子路由的时候步骤条会 +1,
但是浏览器点击返回按钮的话页面是返回去了 但是步骤条没有 -1,
通过使用把步骤条存储在 vuex + 路由守卫解决。

router.js

引入 vuex
import store from '../vuex/store'
Vue.use(store)

// 关键部分路由
{
  path: '/Five/details/Roommates', 
  name: 'DetailsRoommates',
  component: DetailsRoommates,
  children: [
    {
      path: '/Five/details/Roommates/One',
      name: 'DetailsRoommatesOne',
      component: DetailsRoommatesOne,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/Five/details/Roommates/Two',
      name: 'DetailsMasterFilesTwo',
      component: DetailsMasterFilesTwo,
      meta: {
        keepAlive: true
      },
      // 使用 独享守卫  需要写好几遍所有还是用了全局守卫
      // beforeEnter: (to, from, next) => {
      //   store.commit('addActive', 2)
      //   console.log(to, "独享守卫", from, next);
      //   next()
      // }
    },
    {
      path: '/Five/details/Roommates/Three',
      name: 'DetailsRoommatesThree',
      component: DetailsRoommatesThree,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/Five/details/Roommates/Four',
      name: 'DetailsMasterFilesFive',
      component: DetailsMasterFilesFive,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/Five/details/Roommates/Five',
      name: 'DetailsMasterFilesSix',
      component: DetailsMasterFilesSix,
      meta: {
        keepAlive: true
      }
    }
  ]
},

路由守卫

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  console.log(to);

  switch (to.fullPath) {
    case "/Five/details/Roommates/One":
      store.commit('addActive', 1)
      break;
    case "/Five/details/Roommates/Two":
      store.commit('addActive', 2)
      break;
    case "/Five/details/Roommates/Three":
      store.commit('addActive', 3)
      break;
    case "/Five/details/Roommates/Four":
      store.commit('addActive', 4)
      break;
    case "/Five/details/Roommates/Five":
      store.commit('addActive', 5)
      break;
  }
  next()
})

主路由文件关键部分代码

  computed: {
    active() {
      console.log("计算属性");
      return this.$store.state.active;
    }
  },
  methods: {
    ...mapMutations({
      update: "addActive" 
    }),
    next() {
      this.update(this.$store.state.active + 1);
      if (this.active == 1) {
        this.$router.push({ path: "One" });
      }
      if (this.active == 2) {
        this.$router.push({ path: "Two" });
      }
      if (this.active == 3) {
        this.$router.push({ path: "Three" });
      }
      if (this.active == 4) {
        this.$router.push({ path: "Four" });
      }
      if (this.active == 5) {
        this.$router.push({ path: "Five" });
      }
      if (this.active > 5) {
        this.active = 5;
        this.$router.push({ path: "/homes/index" });
      }
      console.log(this.active);
    },
    Previous() {
      this.$router.go(-1);
      // console.log("路由倒退");
    },
  }

这样写的话 点击下一步 vuex 就修改两次 不知道是 让他修改两次比较节能 还是 在 switch case 里判断一下比较节能

相关文章

  • vue 解决浏览器后退路由变化,父路由状态不变问题

    中间的是子路由 跳转到子路由的时候步骤条会 +1,但是浏览器点击返回按钮的话页面是返回去了 但是步骤条没有 -1...

  • 复习3

    vue路由懒加载 1.vue路由懒加载解决的什么 问题:解决打包后文件过大的问题,从而优化页面加载,提升性能 注意...

  • Vue-在vue中如何使用vue-router

    vue-router作为vue的生态系统一员,帮我们解决了项目中路由的相关问题,包括路由切换,路由拦截等问题。此文...

  • Vue2.0一个login跳转实例

    需要解决的问题:store存储登录状态Vue-Router导航钩子拦截路由Vue-Resource获取后台的数据需...

  • Vue-router路由

    vue 监听路由变化 vue-router 由hash向history模式变迁 什么是路由 后端路由:对于普通的网...

  • 18、 vue-router导航解析及钩子函数

    1、vue-router如何响应 路由参数 的变化? 问题:当使用路由参数时,例如从 /content?id=1 ...

  • Named Route '首页' has a default c

    当某个路由有子集路由的时候,这时候父级路由需要一个默认的路由,所以父级路由不能定义name属性。解决办法:即去除父...

  • vue路由跳转路径错误时出现空白页

    参考:解决vue路由跳转未匹配路径时出现空白页的问题在进行vue项目开发时,常用vue-router进行路由的导航...

  • 2020-09-22

    VUE 1. vue 滚动行为用法,进入路由需要滚动到浏览器底部 头部等等 使用场景:使用前端路由,当切换到新路由...

  • Vue-Router 导航守卫

    导航守卫 导航表示路由正在发生改变。(变化的路由)导航守卫就是变化的路由钩子。路由钩子的意思可以理解为vue-ro...

网友评论

      本文标题:vue 解决浏览器后退路由变化,父路由状态不变问题

      本文链接:https://www.haomeiwen.com/subject/jkzggqtx.html