Skip to content

Instantly share code, notes, and snippets.

@arbing
Last active November 21, 2017 07:31
Show Gist options
  • Save arbing/623bda85e975b25f79f0e8b3f47b28ba to your computer and use it in GitHub Desktop.
Save arbing/623bda85e975b25f79f0e8b3f47b28ba to your computer and use it in GitHub Desktop.
vue-router-isBack
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()
})
<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