<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
首先做一個頁面:
新增 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>
預設插槽
父元件中:
<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>
作用域插槽
父元件中:
<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!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45