I just learnt about slot today on Vue official tutorial. As I read through, I came across this example on scoped slot: https://vuejs.org/v2/guide/components.html#Scoped-Slots.
I experimented a little by adding an input field to each item.
Parent:
<my-list :items="items">
<template slot="item" scope="props">
<li class="my-fancy-item">{{ props.text }}</li>
</template>
</my-list>
Child template:
<ul>
<slot name="item" v-for="item in items" :text="item.text">
</slot>
<div> // this will appear at the end of list
<input v-model = "message">
</div>
<p>{{message}}</p>
</ul>
My first attempt was to move the input to the parent scope and called a child function by passing into it the index and input value using props to modify the original array. It worked as intended.
Another attempt is to do binding on parent scope but it won't work because parent scope can't see child property: https://vuejs.org/v2/guide/components.html#Compilation-Scope
What is the best way to insert this input so that it will appear in every item and still be able to bind input to child property?
message
come from? Are you really wanting to have an input that can edit eachitem.text
? – Bertmessage
comes from child. I want to bind the input tomessage
. I am just curious if it is possible to have each editableitem.text
and still use scoped slot. – David