Last active
November 21, 2017 07:31
-
-
Save arbing/623bda85e975b25f79f0e8b3f47b28ba to your computer and use it in GitHub Desktop.
vue-router-isBack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let routerList = [] | |
router.beforeEach((to, from, next) => { | |
if ( | |
routerList.length && | |
routerList.indexOf(to.name) === routerList.length - 1 | |
) { | |
// 后退 | |
routerList.splice(routerList.length - 1, 1) | |
to.meta.isBack = true | |
} else { | |
// 前进 | |
routerList.push(from.name || '/') | |
to.meta.isBack = false | |
} | |
next() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export default { | |
name: 'ScrollPage', | |
data () { | |
return { | |
scrollTop: 0 | |
} | |
}, | |
created () { | |
}, | |
mounted () { | |
this.scrollTop = 0 | |
}, | |
activated () { | |
// 前进刷新数据 | |
if (this.$route.meta.isBack !== undefined && !this.$route.meta.isBack) { | |
this.fetchData() | |
this.scrollTop = 0 | |
} | |
// 后退恢复滚动条位置 | |
document.body.scrollTop = this.scrollTop | |
}, | |
deactivated () { | |
// 离开保存滚动条位置 | |
this.scrollTop = document.body.scrollTop | |
}, | |
methods: { | |
fetchData () {} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment