Home > Article > Web Front-end > What problem does vue slot solve?
The problem solved by the vue slot: Content is not allowed to be written in the middle of the introduced sub-component tags. Slot is a capability provided by Vue for component packagers; it allows developers to define uncertain parts that are expected to be specified by the user as slots when packaging components; slots can be considered as during component packaging. A placeholder for content reserved for the user.
The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.
We know that in vue, content is not allowed to be written in the middle of the introduced sub-component tags. In order to solve this problem, the official introduced the concept of slot.
Slots are actually equivalent to placeholders. It gives your HTML template a place in the component, allowing you to pass in some stuff. Slots are divided into anonymous slots, named slots and scoped slots.
You may not quite understand why I need to pass HTML into the subcomponent instead of writing it directly in the subcomponent? The answer is this. You can imagine a scenario where you have five pages. Only one area of the five pages has different content. How would you write these five pages? Copy and paste is one way, but in vue, slots are better.
Example:
The file directory is as follows, and the Home component is the parent component of HelloWorld.<template> <div> Helloworld组件 <div> <slot></slot> </div> </div> </template> <script> export default { } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style> .hello{ width:100%; height:300px; background:#ccc; margin-top:50px; .slotTxt{ width:500px; height:200px; margin:30px auto; background:red; } } </style>
<template> <div> 我是Home父组件 <helloworld> <!-- 没有插槽,这里的内容不显示 --> <h1>我是helloworld中的插槽啊</h1> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script>Effect It is not difficult to see that the content (red part) in the HelloWorld tag has been displayed.
<template> <div> Helloworld组件 <div> <slot></slot> </div> <div> <slot></slot> </div> </div> </template> <script> export default { } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style> .hello{ width:700px; height:300px; background:#ccc; margin: 0 auto; margin-top:50px; .slotLeft{ width:300px; height:200px; float:left; background:red; } .slotRight{ width:300px; height:200px; float:right; background:pink; } } </style>
<template> <div> 我是Home父组件 <helloworld> <template> <h1>name属性为left</h1> </template> <template> <h1>name属性为right</h1> </template> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
Note that v-slot can only be added on the template tag (with one exception).
<template> <div> 我是Home父组件 <helloworld> <h1>name属性为left</h1> <h1>name属性为right</h1> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
In order to make the data in the child component available in the parent's slot content, we can use the data as the element's A property is bound:
语法:v-bind:users="user"Subcomponent HelloWorld code
<template> <div> Helloworld组件 <div> <slot></slot> </div> </div> </template> <script> export default { data(){ return{ user:{ name:'oralinge', age:18 } } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style> .hello{ width:700px; height:300px; background:#ccc; margin: 0 auto; margin-top:50px; .slotLeft{ width:300px; height:200px; // float:left; background:red; margin:20px auto } .slotRight{ width:300px; height:200px; float:right; background:pink; } } </style>
语法:v-slot:default="随意取的名字" // default可省略,简写为v-slot="随意取的名字"Parent component Home code
<template> <div> 我是Home父组件 <helloworld> <template> <h1>{{slotProps.users.name}}</h1> </template> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
The users in the sub-component are chosen at will, corresponding to the users in the parent component.
The user in the subcomponent is data.
Effect
The same as the anonymous slot, only You need to replace default with the name value of the slot.
Sub component HelloWorld code<template> <div> Helloworld组件 <div> <slot></slot> </div> </div> </template> <script> export default { data(){ return{ user:{ name:'hello world', age:18 } } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style> .hello{ width:700px; height:300px; background:#ccc; margin: 0 auto; margin-top:50px; .slotLeft{ width:300px; height:200px; // float:left; background:red; margin:20px auto } .slotRight{ width:300px; height:200px; float:right; background:pink; } } </style>
<template> <div> 我是Home父组件 <helloworld> <template> <h1>{{slotProps.users.name}}</h1> </template> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
注意:
默认插槽的缩写语法不能和具名插槽混用,因为它会导致作用域不明确。
另,slot-scope写法在2.6之后已废弃,作用与上面相同,在此不做解释。
上面的写法是不是觉得有些麻烦?别着急,我们来看一看解构插槽 Prop。
解构插槽 Prop
作用域插槽的内部工作原理是将你的插槽内容包括在一个传入单个参数的函数里:
function (slotProps) { // 插槽内容 }
这意味着 v-slot 的值实际上可以是任何能够作为函数定义中的参数的 JavaScript 表达式。所以在支持的环境下 (单文件组件或现代浏览器),你也可以使用 ES2015 解构来传入具体的插槽 prop。
语法:v-slot="{ users }"
<template> <div> Helloworld组件 <div> <slot></slot> </div> </div> </template> <script> export default { data(){ return{ user:{ name:'hello world', age:18 } } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style> .hello{ width:700px; height:300px; background:#ccc; margin: 0 auto; margin-top:50px; .slotLeft{ width:300px; height:200px; // float:left; background:red; margin:20px auto } .slotRight{ width:300px; height:200px; float:right; background:pink; } } </style>
<template> <div> 我是Home父组件 <helloworld> <template> <h1>{{users.name}}</h1> </template> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
效果
<template> <div> 我是Home父组件 <helloworld> <template> <h1>{{person.name}}</h1> </template> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
效果如上图。
<template> <div> 我是Home父组件 <helloworld> <template> <h1>{{users.name}}</h1> </template> </helloworld> </div> </template> <script> import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'home', components: { HelloWorld } } </script> <style> .home{ width:900px; margin:0 auto; background:yellow; padding-bottom:100px; } </style>
<template> <div> <div> <span>{{title}}</span> <div> <slot></slot> </div> </div> <div> <slot></slot> </div> </div> </template> <script> export default { data () { return { } }, props: { title: { type: String, required: true } } } </script> <style> .title-box { padding: 16px 0; border-bottom: 1px solid #eff1f5; .title { font-family: MicrosoftYaHei; font-size: 24px; color: #283039; letter-spacing: 0; line-height: 24px; &::before { width: 4px; margin-right: 20px; content: ""; background-color: #5da1ff; display: inline-block; height: 20px; vertical-align: middle; } } .right { float: right; margin-right: 20px; } } </style>
使用的ui框架为ivew。
相关推荐:vue.js视频教程
The above is the detailed content of What problem does vue slot solve?. For more information, please follow other related articles on the PHP Chinese website!