Skip to content

onActivated not triggered in child component with lazy mount #10806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DragonnZhang opened this issue Apr 26, 2024 · 6 comments
Open

onActivated not triggered in child component with lazy mount #10806

DragonnZhang opened this issue Apr 26, 2024 · 6 comments
Labels
has workaround A workaround has been found to avoid the problem need guidance The approach/solution in the PR is unclear and requires guidance from maintainer to proceed further. scope: keep-alive

Comments

@skirtles-code
Copy link
Contributor

Seems similar to #7276, though not exactly the same. It might be the same underlying cause.

That other issue has a PR at #7286, but that seems to be only handling cases involving defineAsyncComponent, so it doesn't fix the issue reported here.

@quiteeasy
Copy link
Contributor

quiteeasy commented Apr 26, 2024

The issue goes deeper than the onActivated lifecycle hook not being triggered in async context. The onActivated is not called on all descendant components mounted after a v-if change. I've prepared a demo that illustrates the behavior.

This raises a question about whether the observed results align with the expected behavior or if there might be an inconsistency or potential issue with the way descendant components are handled in this scenario. If using v-show everything works as expected. Seems to be related to mounting components in different ticks from KeepAlive's tick, seemingly they are not cached in KeepAlive as well.

Edit: As a potential workaround for the issue where onActivated is not called on descendant components after a v-if change, you can consider using v-show instead. This seems to resolve the problem and ensures the expected behavior, as the lifecycle hooks are triggered correctly when components are toggled with v-show.

@DragonnZhang
Copy link
Author

The issue goes deeper than the onActivated lifecycle hook not being triggered in async context. The onActivated is not called on all descendant components mounted after a v-if change. I've prepared a demo that illustrates the behavior.

This raises a question about whether the observed results align with the expected behavior or if there might be an inconsistency or potential issue with the way descendant components are handled in this scenario. If using v-show everything works as expected. Seems to be related to mounting components in different ticks from KeepAlive's tick, seemingly they are not cached in KeepAlive as well.

Edit: As a potential workaround for the issue where onActivated is not called on descendant components after a v-if change, you can consider using v-show instead. This seems to resolve the problem and ensures the expected behavior, as the lifecycle hooks are triggered correctly when components are toggled with v-show.

You are right. I will try v-show instead. Thank you.

@quiteeasy
Copy link
Contributor

The Vue documentation states that the onActivated and onDeactivated hooks should work not only for the root component cached by <KeepAlive>, but also for descendant components in the cached tree. However, the issue we're facing is that onActivated is not being called on conditional descendants (those toggled with v-if) of cached components.

This raises a couple of questions:

  1. Should we expect onActivated/onDeactivated to be called on conditional descendants of cached components, as per the documentation? Or should we stick to using regular lifecycle hooks for these cases?
  2. If this behavior is considered an inconsistency or a bug, should we document this particular case or try to fix it?

I'm ready to provide a PR to address this issue, but I'd like to get feedback from the team first on the expected behavior and the best approach moving forward.

@DragonnZhang
Copy link
Author

I found an easier method to trigger onActivated. Instead of using v-show, just wrap the component with another <KeepAlive>, like this.

@edison1105 edison1105 added scope: keep-alive 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. need guidance The approach/solution in the PR is unclear and requires guidance from maintainer to proceed further. and removed 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Aug 8, 2024
@edison1105 edison1105 changed the title Activated lifecycle hook not triggered in async context onActivated not triggered in child component with lazy mount Aug 9, 2024
@edison1105 edison1105 added the has workaround A workaround has been found to avoid the problem label Aug 9, 2024
@edison1105
Copy link
Member

Note: This behavior is consistent with vue2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround A workaround has been found to avoid the problem need guidance The approach/solution in the PR is unclear and requires guidance from maintainer to proceed further. scope: keep-alive
Projects
None yet
Development

No branches or pull requests

4 participants