1. 程式人生 > 程式設計 >Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

1、slot作用/概念:預先將將來要使用的內容進行保留;

2、具名插槽:給slot起個名字

3、slot、slot-scope已經被廢棄推薦使用vue2.6.0中的v-slot;但是這邊還是對新舊方法對做一下使用說明。

slot插槽(不具名)

<body>
 <div id="app">
  <Test>
   <div>slot插槽佔位內容</div>
  </Test>
 </div>
 <template id="test">
  <div>
   <slot></slot>//定義插槽
   <h3>這裡是test元件</h3>
  </div>
 </template> 
</body>

<script>
 Vue.component('Test',{
  template:"#test"
 });

 new Vue({
  el:"#app",})
</script>

Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

slot具名插槽使用

在元件中使用slot進行佔位時,在slot標籤內使用name 屬性給slot插槽定義一個名字,就是具名插槽。在html中使用具名插槽時,使用slot引入

<body>
 <div id="app">
  <Test>
   <div slot="header">這裡是頭部</div>//具名插槽使用
   <div slot="footer">這裡是尾部</div>
  </Test>
 </div>
 <template id="test">
  <div>
   <slot name="header"></slot>//具名插槽
   <h3>這裡是Test元件</h3>
   <slot name="footer"></slot>
  </div>

 </template>
</body>
<script>
 Vue.component(
  'Test',{
   template:"#test"
 });
 new Vue({
  el:"#app"
 })
</script>

Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

v-slot使用

v-slot在元件中使用slot進行佔位時,也是在slot標籤內使用name 屬性給slot插槽定義一個名字。但是在html內使用時就有些不同了。需要使用template模板標籤,template標籤內,使用v-slot指令繫結插槽名,標籤內寫入需要新增的內容

<body>
 <div id="app">
  <Test>
   <template v-slot:header>//v-slot指令使用插槽
   <h2>slot頭部內容</h2>
   </template>
   
   <p>直接插入元件的內容</p>
   
   <template v-slot:footer>
   <h2>slot尾部內容</h2>
   </template>
  </Test>
 </div>
 
 <template id ='test'>
  <div class="container">
   <header>
   <!-- 我們希望把頁頭放這裡 -->
   <slot name = "header"></slot>//具名插槽
   </header>
   <section>
   主體內容部分
   </section>
   <footer>
   <!-- 我們希望把頁尾放這裡 -->
   <slot name = 'footer'></slot>
   </footer>
  </div>
 </template> 
</body>

<script>
 Vue.component('Test',{
  template:"#test"
 });
 new Vue({
  el:"#app"
 })
</script>

Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

作用域插槽:

slot-scope使用:

a、:在元件模板中書寫所需slot插槽,並將當前元件的資料通過v-bind繫結在slot標籤上。

b、:在元件使用時,通過slot-scope=“gain”,接收元件中slot標籤上繫結的資料。

c、:通過gain.xxx就可以使用繫結資料了

<body>
 <div id="app">
  <Test>
   <div slot="default" slot-scope="gain">//作用域插槽的用法(slot-scope)
   {{ gain.msg }}
   </div>
   
  </Test>
 </div>

 <template id="test">
  <div>
   <slot name="default" :msg="msg"> </slot>
   <p>這裡是test元件</p>
  </div>
 </template>
</body>
<script>
 new Vue({
  el:"#app",components:{
   'Test':{
   template:"#test",data(){
    return {
     msg:"你好"
    }
   },}
  }
 })
</script>

Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

作用域插槽:v-slot的用法

<body>
 
 <div id="app">
  <Test>
   <template v-slot:header="gain">//v-slot定義作用域插槽
   <div>
     <h3>slot</h3>
     <p> {{gain.msg}} </p>
   </div>
   </template>
   
  </Test>
 </div>
 
 <template id="test">
  <div>
   <slot name="header":msg="msg"></slot>
   <p>這裡是test元件</p>
  </div>
 </template>
 
</body>
<script>
 Vue.component('Test',{
  template:"#test",data(){
   return {
   msg:'這裡是頭部'
   }
  }
 });

 new Vue({

 }).$mount("#app")
</script>

Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

Vue2.6.0中使用v-slot指令取代了特殊特性slot與slot-scope,但是從上述案例可以看出,v-slot在使用時,需要在template標籤內,這點大家要注意。

補充知識:vue中v-slot指令如何應用Vue插槽及與slot、slot-scope的用法區別

不具名插槽

子元件:

<template>
 <div>
 <!--定義不具名插槽 -->
 <slot></slot>
 <h3>這裡是不具名插槽元件</h3>
 </div>
</template>
 
<script>
export default {
 data() {
 return {}
 },created() {},mounted() {},methods: {}
}
</script>
<style lang="scss" scoped></style>

在父元件中使用:

<template>
 <div id="inforCategory-warp">
 <!-- 不具名插槽 -->
 <lxsolt>不具名插槽</lxsolt>
 
 </div>
</template>
 
<script>
import lxsolt from './lx'
export default {
 name: 'inforCategory',components: {
 lxsolt,},data(){
 return{}
 }
}
</script>
<style lang="scss" scoped>
#inforCategory-warp {
}
</style>

作用域插槽:

slot-scope使用(slot-scope繫結的是子元件的資料):

在元件模板中書寫所需slot插槽,並將當前元件的資料通過v-bind繫結在slot標籤上。

在元件使用時,通過slot-scope=“scope”,接收元件中slot標籤上繫結的資料。

通過scope.xxx就可以使用繫結資料了

具名插槽以及作用域插槽

子元件:

<template>
 <div>
 <slot name="header" :msg="name"></slot>
 <h3>這裡是具名插槽元件</h3>
 <slot name="footer"></slot>
 </div>
</template>
 
<script>
export default {
 data() {
 return {
  name:'具名插槽元件'
 }
 },methods: {}
}
</script>
<style lang="scss" scoped></style>

父元件:

<template>
 <div id="inforCategory-warp">
  <!-- 具名插槽 -->
 <nameSlot>
  <div slot="header" slot-scope="scope">這裡是slot-scope<span style="color:red">{{scope.msg}}</span>頭部</div>
  <div slot="footer">這裡是尾部</div>
 </nameSlot>
 
 </div>
</template>
 
<script>
import nameSlot from './nameSlot'
export default {
 name: 'inforCategory',components: {
 nameSlot,data(){
 return{
  msg:'具名插槽資訊',msg2:'v-slot'
 }
 }
}
</script>
<style lang="scss" scoped>
#inforCategory-warp {
}
</style>

v-slot以及作用域插槽

子元件:

<template>
 <div>
 <div class="container">
  <header>
  <!-- 我們希望把頁頭放這裡 -->
  <slot name="header"></slot>
  </header>
  <section>
  v-slot元件
  </section>
  <footer>
  <!-- 我們希望把頁尾放這裡 -->
  <slot name="footer" :msg="msg"></slot>
  </footer>
 </div>
 </div>
</template>
 
<script>
export default {
 data() {
 return {
  msg:'vsolt作用域插槽元件'
 }
 },methods: {}
}
</script>
<style lang="scss" scoped></style>

父元件:

<template>
 <div id="inforCategory-warp">
 <vsolt>
  <template v-slot:header>
  <h2>slot頭部內容</h2>
  </template>
 
  <p>直接插入元件的內容</p>
 
  <template v-slot:footer="scope">
  <h2>slot尾部內容<span style="color:red">{{scope.msg}}</span></h2>
  </template>
 </vsolt>
 
 </div>
</template>
 
<script>
import vsolt from './v-slot'
export default {
 name: 'inforCategory',components: {
 vsolt,msg2:'v-slot'
 }
 }
}
</script>
<style lang="scss" scoped>
#inforCategory-warp {
}
</style>

以上這篇Vue插槽_特殊特性slot,slot-scope與指令v-slot說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。