Use slot-scope to copy slot content in vue

Source: Internet
Author: User

Sometimes our vue component needs to copy the content transmitted by the user.

For example, the carousel components in our project need to use the replicated slots to achieve the cyclic scrolling effect.

The user pays attention to the static effect of the carousel content, and the component is responsible for rolling it up.

Component: & lt; Div class = "horse-lamp" & gt; & lt; Div class = "lamp" & gt; & lt; slot name = "lamps" & gt; & lt;/slot & gt; & lt;/Div & gt; & lt; Div class = "lamp" & gt; & lt; slot name = "lamps" & gt; & lt;/slot & gt; & lt;/Div & gt; User: & lt; carouselwidget & gt; & lt; div slot = "lamps" & gt; 123 & lt;/Div & gt; & lt;/carouselwidget & gt;

This implementation method is fine for initialized data, but does not support dynamic binding of Slot content. This is because

  • Vnode is unique in vue
  • A vnode is bound to only one dom node.

Therefore, when the Component User declares a node, because only one div is declared, only one vnode is generated in the background, although two divs are generated, however, vnode is bound only to the next Dom. When vnode is modified, only one dom is modified.

Solution: Slot-Scope
Each slot generated using slot-scope data generates a new vnode.

Component: & lt; Div class = "horse-lamp" & gt; & lt; Div class = "lamp" & gt; & lt; slot name = "lamps ": arr = "arr" & gt; & lt;/slot & gt; & lt;/Div & gt; & lt; Div class = "lamp" & gt; & lt; slot name = "lamps": arr = "arr" & gt; & lt;/slot & gt; & lt;/Div & gt; user: & lt; carouselwidget: arr = "info. column "& gt; & lt; Template scope =" slotprops "slot =" lamps "& gt; & lt; component V-for =" (item, index) in slotprops. arr ": Key =" info. ID ": Is =" templates [item. type] ": item =" item "& gt; & lt;/component & gt; & lt;/template & gt; & lt;/carouselwidget & gt;

In this case, the component content is changed, so the component content will be updated simultaneously in two slots.

The vue version used by the project is 2.4.2. The slot-scope syntax of the more advanced vue may be different.

Original article address: 1190000016747615

Use slot-scope to copy slot content in vue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.