Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

在选项式的写法中组件的守卫事件无法被正确调用 #2505

Unanswered
AiFu-o asked this question in Help and Questions
Discussion options

Reproduction

4.*

Steps to reproduce the bug

在选项式的页面中注册页面的子组件中调用守卫事件beforeRouteLeave
但是在声明式的页面中的子组件调用onBeforeRouteLeave就可以

Expected behavior

在注册页面的子组件中调用守卫事件生效

Actual behavior

在注册页面的子组件中调用守卫事件不生效

Additional information

排查源码发现问题在于选项式的守卫事件只是直接调用了 component 的 守卫事件方法 而这种方式无法对组件中的子组件进行扫描
但是在声明式中是把onBeforeRouteLeave直接注册成了事件 是组件主动调用的 而不是 vue-router 自动获取的 目前的解决办法是在 vue 的 created 或 mounted生命周期中调用onBeforeRouteLeave 手动注册事件
希望可以更新文档或使用其他方式扫描选项式的守卫事件

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #2504 on May 27, 2025 13:30.

AltStyle によって変換されたページ (->オリジナル) /