Skip to content

比如,从页面 A 出发,出发时,页面 A 有几个tabs,默认值是 tabs—1,离开页面 A 时在 tabs—3,此时,你触发了某个事件,进行了页面跳转,然后跳转到新页面 B 中,然后你点击了页面 B返回 button,执行了以下JS语句:

js
this.$store.dispatch('tagsView/delView', this.$route)
this.$router.go(-1)
this.$store.dispatch('tagsView/delView', this.$route)
this.$router.go(-1)

你发现你虽然回到了页面 A ,但是此时的tabs不是 tabs—3,而是 tabs—1。那么,怎么让路由跳转并且返回时,记住出发时的状态呢?

需要分别对不同的路径,进行不同的操作

做路由监听变化即可

js
watch : {
    '$route' (to, from) {
        // from 对象中要 router 来源信息.
        // do your want
    }
}
watch : {
    '$route' (to, from) {
        // from 对象中要 router 来源信息.
        // do your want
    }
}
js
beforeRouteEnter (to, from, next) {
       console.log(to)
       console.log(from)
       console.log(next)
       next(); // 需要添加next(),才能进行下一步
}
beforeRouteEnter (to, from, next) {
       console.log(to)
       console.log(from)
       console.log(next)
       next(); // 需要添加next(),才能进行下一步
}