Home>Article>Web Front-end> What is a slot? Let’s talk about how to use slots in Vue3
VueI believe that everyone who has used Vue has used the slots more or less, but do you know all its uses? This article will bring you all the usage of slots in Vue3 to help you find and fill gaps. (Learning video sharing:vue video tutorial)
Simply speaking, it is the provided in the sub-component Apit
used by the parent component, represented by
. The parent component can fill in any template code in this pit and then ## in the child component #will be replaced with these contents. For example, in the simplest slot example
//父组件, the//子组件ChildHello Juejin 1
2
in the child component is the parent component placed between the child component tag
the content of the space. Of course, you can pass in any code snippet during this period, and it will be placed in the
position.
##Similarly, you can also put variables between the tags
, such as
Let me explain the following first The two words that appear frequently//父组件 <template> <div> <child>{{ msg }}</child> </div> </template> <script> import { ref } from 'vue' import Child from './Child.vue' const msg = ref('Hello Juejin') </script>
andslot content
prevent confusion in subsequent readings:
Same
Slotrepresents thismsg
variable. Therefore, the child componentslot
can access the data scope of the parent component, whileslot content
cannot access the data of the child component (that is, the two< in the parent component Data in child components cannot be used between ;Child>
), this is the so-called rendering scope. The method ofslot
passing parameters toslot content
will be introduced later
is specified, we can specify the default content for theslot
of the child component, such as
at this time//子组件 <template> <div> <slot>我是默认内容</slot> </div> </template> //父组件1 <template> <div> <child></child> </div> </template> <script> import Child from './Child.vue' </script> //父组件2 <template> <div> <child>Hello Juejin</child> </div> </template> <script> import Child from './Child.vue' </script>
Display default content
Display provided content
cannot meet our needs, we need multipleslots
. So there isnamed slot
, which is aslot
with a name. To put it simply, the purpose of thisNamed Slot
is to trap a carrot and let them stay where they should. For example, a slot withname
is called a named slot.
for whichname
is not provided will be implicitly named "default". In the parent component, you can use thev-slot:xxx
(can be abbreviated as#xxx
) directive'selement to pass the name of the target slot. Go down and match the corresponding
slot
. For example,//子组件 <template> <div> <!-- 大萝卜 --> <div> <slot></slot> </div> <!-- 小萝卜 --> <div> <slot></slot> </div> <!-- 中萝卜 --> <div> <slot></slot> </div> </div> </template> //父组件 <template> <div> <child> <!-- #smallTurnip 为v-slot:smallTurnip缩写 --> <template> 小萝卜 </template> <template> 中萝卜 </template> <template> 大萝卜 </template> </child> </div> </template> <script> import Child from './Child.vue' </script>
So there is no need to care about the order in the parent component. You only need to write the template and name it, and it will automatically go to its corresponding location.
Dynamic slot nameor abbreviated as#[variable name]
.//父组件 <template> <div> <child> <!-- 等同于#smallTurnip --> <template> 小萝卜 </template> <template> 中萝卜 </template> <template> 大萝卜 </template> </child> </div> </template> <script> import { ref } from 'vue' import Child from './Child.vue' const slotName = ref('smallTurnip') </script>
It is impossible to access the data of subcomponents, but what if we want to access the status of subcomponents inSlot content
?In fact,
can be passed to theslot
tag by binding properties to theslot content## in the parent component just like passing props to the component. #. First, let’s look at the value transfer method of the default slot
//子组件//父组件My name is {{ slotProps.personName }} and I am {{ slotProps.age }} years old this year
You can also get the data provided by
slot in the form of a structure
My name is {{ personName }} and I am {{ age }} years old this year
注意不能绑定name
属性,因为你绑定了name
它就成了具名插槽了。同样具名插槽中的name
属性也不会传递给插槽内容
。因为传递的参数只能在插槽内容
中使用,所以这类能够接受参数的插槽就被称为了作用域插槽
。
下面再看下具名作用域插槽
它的传参方式。它接收参数的方式是通过template
标签的指令v-slot
的值获取的,所以可以缩写成这样
//父组件//子组件Child.vue{{ bigTurnipProps.message }}
这类插槽便是具名作用域插槽
啦
到这里插槽
(slot)的全部用法基本就已经介绍完啦。如果感觉对你有用的话赶紧点赞收藏吧!
The above is the detailed content of What is a slot? Let’s talk about how to use slots in Vue3. For more information, please follow other related articles on the PHP Chinese website!