Skip to content
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

fix(components): [carousel] Watch items causing state loss(#13010) #13011

Merged
merged 3 commits into from
Sep 16, 2023
Merged

fix(components): [carousel] Watch items causing state loss(#13010) #13011

merged 3 commits into from
Sep 16, 2023

Conversation

SaberA1ter
Copy link
Contributor

@SaberA1ter SaberA1ter commented May 30, 2023

Please make sure these boxes are checked before submitting your PR, thank you!

  • Make sure you follow contributing guide English | (中文 | Español | Français).
  • Make sure you are merging your commits to dev branch.
  • Add some descriptions and refer to relative issues for your PR.

Description

在 carousel 中,
carousel-item 在 onMounted 中向 carousel 中的 items 添加自己,
carousel 添加 watch 去监听 items 变化,每当变化时都会执行 setActiveItem(props.initialIndex) 重置当前播放状态 activeIndex

这个流程在实际执行中存在问题,他的执行顺序为:

carousel-item onMounted -> 上层 onMounted -> watch items(置为默认状态)

这就导致 issue 中翻页失效问题,因为在 watch 中原本应该已经翻页的页数又被重置为 initialIndex。

我将 watch(items) 放在 carousel onMounted 中,但由于 items 的修改在 carousel-item onMounted 又会被触发,所以需要手动设置 activeIndex

最终代码执行顺序:

carousel-item onMounted -> carousel onMounted(置为默认状态) -> 上层 onMounted -> watch items(如果存在变化的话)

Related Issue

Fixes #13010 .

Explanation of Changes

🤖 Generated by Copilot at 0f31682

  • Fix carousel initial item bug by moving watch effect for items.value inside onMounted hook and calling setActiveItem (link)

@pull-request-triage
Copy link

👋 @SaberA1ter, seems like this is your first time contribution to element-plus.

  • Please make sure that you have read our guidelines and code of conduct before making a contribution.
  • You can comment with /label Components:[component_name] to add a label for which component you are working on.
  • You may join our Discord for staying tuned.

@pull-request-triage pull-request-triage bot added 1st contribution Their very first contribution Needs Review labels May 30, 2023
@github-actions
Copy link

github-actions bot commented May 30, 2023

@github-actions
Copy link

Hello @SaberA1ter, thank you for contributing to element-plus, please see our guideline to see how to make contribution

@github-actions
Copy link

github-actions bot commented May 31, 2023

🧪 Playground Preview: https://element-plus.run/?pr=13011
Please comment the example via this playground if needed.

Copy link
Member

@HeftyKoo HeftyKoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HeftyKoo HeftyKoo merged commit 14c43ca into element-plus:dev Sep 16, 2023
10 checks passed
@element-bot element-bot mentioned this pull request Oct 13, 2023
3 tasks
consultation-applio pushed a commit to consultation-applio/element-plus that referenced this pull request Nov 10, 2023
…us#13010) (element-plus#13011)

* fix(components): [carousel] Watch items causing state loss(element-plus#13010)

* perf(components): [carousel] Using the third parameter of watch(element-plus#13010)

---------

Co-authored-by: ZZQChongYa <zhengziqiang@star-net.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Component] [carousel] 在 onMounted 中执行翻页操作失效
3 participants