首頁 > 軟體

Vue 中插槽的使用總結

2022-04-15 13:00:51

預設插槽

首先做一個頁面:

新增 Category.vue

<template>
<div class="category">
  <h3>{{title}}分類</h3>
  <ul>
    <li v-for="(item,index) in listData" :key="index">{{item}}</li>
  </ul>
</div>
</template>

<script>
export default {
  name: "Category",
  props:["title","listData"]
}
</script>

<style scoped>
.category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3{
  text-align: center;
  background-color: orange;
}
</style>

App.vue 中使用

<template>
  <div class="container">
    <Category title="美食" :listData="foods"/>
    <Category title="遊戲" :listData="games"/>
    <Category title="電影" :listData="films"/>
  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
  data(){
    return{
      foods:["火鍋","燒烤","小龍蝦","牛排"],
      games:["勁舞團","QQ飛車","超級瑪麗","無人深空"],
      films:["《教父》","《狩獵》","《禁閉島》","《聚焦》"]
    }
  }

}
</script>

<style>
.container {
  display: flex;
  justify-content: space-around;
}
</style>

現在修改需求,每個分類都要展示不同的內容:

這裡就用到了插槽,修改 Category.vue

<template>
<div class="category">
  <h3>{{title}}分類</h3>
  <slot></slot>
</div>
</template>

<script>
export default {
  name: "Category",
  props:["title"]
}
</script>

<style scoped>
.category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3{
  text-align: center;
  background-color: orange;
}
</style>

修改 App.vue

<template>
  <div class="container">
    <Category title="美食" :listData="foods">
      <img src="https://img2.baidu.com/it/u=2073611229,1921777437&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675"/>
    </Category>
    <Category title="遊戲" :listData="games">
      <ul>
        <li v-for="(g,index) in games" :key="index">{{g}}</li>
      </ul>
    </Category>
    <Category title="電影">
      <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"/>
    </Category>
  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
  data(){
    return{
      foods:["火鍋","燒烤","小龍蝦","牛排"],
      games:["勁舞團","QQ飛車","超級瑪麗","無人深空"],
      films:["《教父》","《狩獵》","《禁閉島》","《聚焦》"]
    }
  }

}
</script>

<style>
.container {
  display: flex;
  justify-content: space-around;
}
img,video{
  width: 100%;
}
</style>

具名插槽

修改 Category.vue

<template>
<div class="category">
  <h3>{{title}}分類</h3>
  <slot name="center">這是一些預設值,沒有內容時展示</slot>
  <slot name="footer">這是一些預設值,沒有內容時展示</slot>
</div>
</template>

<script>
export default {
  name: "Category",
  props:["title"]
}
</script>

<style scoped>
.category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3{
  text-align: center;
  background-color: orange;
}
</style>

修改 App.vue

<template>
  <div class="container">
    <Category title="美食" :listData="foods">
      <img slot="center" src="https://img2.baidu.com/it/u=2073611229,1921777437&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675"/>
      <a slot="footer" href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >更多美食</a>
    </Category>
    <Category title="遊戲" :listData="games">
      <ul slot="center">
        <li v-for="(g,index) in games" :key="index">{{g}}</li>
      </ul>
      <div class="foot" slot="footer">
        <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >單機遊戲</a>
        <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >網路遊戲</a>
      </div>
    </Category>
    <Category title="電影">
      <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"/>
      <!--v-slot 只有template能用-->
      <template v-slot:footer>
        <div class="foot" slot="footer">
          <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >經典</a>
          <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >熱門</a>
          <a href="https://www.baidu.com" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >推薦</a>
        </div>
      </template>
    </Category>
  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
  data(){
    return{
      foods:["火鍋","燒烤","小龍蝦","牛排"],
      games:["勁舞團","QQ飛車","超級瑪麗","無人深空"],
      films:["《教父》","《狩獵》","《禁閉島》","《聚焦》"]
    }
  }

}
</script>

<style>
.container,.foot {
  display: flex;
  justify-content: space-around;
}
img,video{
  width: 100%;
}
</style>

作用域插槽

如果資料在 Category 中,但需要展示不同的形式,我們可以通過插槽傳值,類似於我們從父元件向子元件傳值:

<template>
  <div class="category">
    <h3>{{ title }}分類</h3>
    <slot :games="games" :msg="hello"></slot>
  </div>
</template>

<script>
export default {
  name: "Category",
  props: ["title"],
  data() {
    return {
      games: ["勁舞團", "QQ飛車", "超級瑪麗", "無人深空"]
    }
  }
}
</script>

<style scoped>
.category {
  background-color: skyblue;
  width: 200px;
  height: 300px;
}

h3 {
  text-align: center;
  background-color: orange;
}
</style>

App 中在子元件中使用 <template> 包裹要展示的內容,標籤中可以使用scope接收傳過來的值

<template>
  <div class="container">
    <Category title="遊戲">
      <template v-slot="receiveValue">
        <ul>
          <li v-for="(g,index) in receiveValue.games" :key="index">{{ g }}</li>
        </ul>
        <a>{{ receiveValue.msg }}</a>
      </template>
    </Category>

    <Category title="遊戲">
      <template v-slot="{games,msg}">
        <ol>
          <li v-for="(g,index) in games" :key="index">{{ g }}</li>
        </ol>
        <a>{{ msg }}</a>
      </template>
    </Category>

    <Category title="遊戲">
      <template v-slot="{games,msg}">
        <h4 v-for="(g,index) in games" :key="index">{{ g }}</h4>
        <a>{{ msg }}</a>
      </template>
    </Category>

  </div>
</template>

<script>
import Category from "@/components/Category";

export default {
  name: 'App',
  components: {Category},
}
</script>

<style>
.container, .foot {
  display: flex;
  justify-content: space-around;
}

img, video {
  width: 100%;
}
</style>

插槽總結

  • 1.作用:讓父元件可以向子元件指定位置插入 html 結構,也是一種元件問通訊的方式,適用於父元件==>子元件
  • 2.分類:預設插槽、具名插槽、作用域插槽
  • 3.使用方式:

預設插槽

父元件中:

<Category>
    <div>html結構</div>
<Category>

子元件中:

<template>
    <div>
    <!--定義插槽-->
    <slot>插槽預設內容...</slot>
    </div>
</templdte>

具名插槽

父元件中:

<Category>
    <template slot="center">
        <div>html結構1</div>
    </template>
    <tenplate v-slot:footer>
        <div>html結構2</div>
    </template>
</Category>

子元件中:

<template>
    <div>
    <1--定義插槽-->
    <slot name="center">插槽預設內容...</slot>
    <slot name="footer」>插槽預設內容...</slot>
    </div>
</template>

作用域插槽

  • 1.理解:資料在元件的自身,但根據資料生成的結構需要元件的使用者來決定,(games 資料在 Category 元件中,但使用資料所遍歷出來的結構由 App 元件決定)
  • 2.具體編碼:

父元件中:

<category>
    <template v-slot="scopeData"
    <!--生成的是ul列表-->
    <ul>
        <li v-for="g in scopeDsta.games" : key="g">{g}</li>
    </ul>
    </template>
</Category>

<Category>
    <template v-slot={scopeData}>
    <!--生成的是h4標題-->
    <h4 v-for="g in scopeData" :key="g">{{g}}</h4>
    </template>
</Category>

子元件中:

<template>
    <div>
        <slot :games="games"></slot>
    </div>
</template>

<script>
export default{
    name: "Category ',
    props: ["title"],
    //資料在子元件自身
    data() {
        return{
            games:['紅色警戒,穿越火線',"勁舞團",超級瑪麗"]
        }
    }
}</script>

注意:scope和slot-scope過時了,可以使用v-slot

到此這篇關於Vue 中插槽的使用總結的文章就介紹到這了,更多相關Vue 插槽使用內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com