Skip to content

vue-router 组合式API为什么不提供beforeRouterEnter? #2430

Closed Answered by pdsuwwz
LostElkByte asked this question in Help and Questions
Discussion options

You must be logged in to vote

@LostElkByte 你可以尝试在 defineOptions 里面添加 beforeRouteEnter 方法:

defineOptions({
  beforeRouteEnter: async (to, from, next) => {
    // ...
    next()
  },
})

需要注意的是在 beforeRouteEnter 内无法获取到当前组件实例对象,因为当守卫执行时,组件实例还未被创建, 如果你需要记录位置并想在下次获取到上次的位置,你可以使用 pinia 或 全局 hook 的形式来记录数据

这里有一个我整理的 Playground,或许能提供一些参考。

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@elarbor
Comment options

Answer selected by posva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants