Scope usage of slot slots (excerpt from Vue.js official website)

Source: Internet
Author: User

Sometimes you want the supplied component to have a reusable slot that gets the data from the subassembly. For example, a template for a simple <todo-list> component might contain the following code:

<ul>
<Li
V-for="Todo in Todos"
v-bind:key="Todo.id"
>
{{Todo.text}}
</li>
</ul>

But in some parts of our application, we want each individual backlog to render todo.text something that is not quite the same. This is also where the scope slot comes in.

To make this feature possible, all you need to do is wrap the backlog <slot> on an element, and then pass all of its context-sensitive data to the slot: in this case, the data is the todo object:

<Ul>
<Li
V-for="Todo in Todos"
v-bind:key= "Todo.id"
<!--we have a slot for each TODO, -->
<!--to pass the ' Todo ' object as a slot prop. -->
<slot v-bind:todo= "Todo",
<!--fallback content-->
</SLOT>
Span class= "line" > </li>,
< /UL>

Now when we use <todo-list> the component, we can choose to define a different alternative for the to-do item <template> , and we can get the slot-scope data from the subcomponents through the attributes:

<Todo-listv-bind:todos="Todos" >
<!--define ' Slotprops ' as the name of the socket scope-->
<template slot-scope= Span class= "line" > <!--customize a template for Todo items,-->
<span v-if= "SlotProps.todo.isComplete";? </SPAN>
{{SlotProps.todo.text}}
</template>,
</< Span class= "name" >TODO-LIST>

In 2.5.0+, it is slot-scope no longer restricted to <template> use on elements, but can be used on any element or component in a slot.

Deconstruction slot-scope

If a JavaScript expression is valid in a function-defined parameter position, the expression can actually be slot-scope accepted. This means you can use the ES2015 deconstruction syntax in these expressions in a supported environment (single-file component or modern browser). For example:

<v-bind:todos="Todos" >
<slot-scope="{todo}" >
<v-if="Todo.iscomplete";? </span>
{{Todo.text}}
</template>
</todo-list>

This makes the scope slots cleaner.

Scope usage of slot slots (excerpt from Vue.js official website)

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.