Vue插槽---默认插槽、具名插槽、作用域插槽

浅浅的花香味﹌ 2024-04-06 10:08 238阅读 0赞

目录

总结

1.默认插槽

2.具名插槽

3.作用域插槽

一、不使用插槽的效果

App.vue

Category.vue

二、默认插槽

App.vue

Category.vue

三、具名插槽

App.vue

Category.vue

四、作用域插槽

App.vue

Category.vue


总结

作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于父组件===》子组件

分类:默认插槽、具名插槽、作用域插槽

使用:

1.默认插槽

553842c8a0aa48d7818f0fd4f521d57e.png

2.具名插槽

6ecc18298ed44b37afceaaafe4e80a31.png

3.作用域插槽

作用域插槽也可以有名字

理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定

fbe4f97ea69b43958fb3acaf27f8b7b3.png

ecdb041c3bad41ddb673edf9c491d394.png

一、不使用插槽的效果

4bda280d818a4dc2ae5ec93bc829b7b9.png

App.vue

  1. <template>
  2. <div class="container">
  3. <Category title="美食" :listData="foods" ></Category>
  4. <Category title="游戏" :listData="games"></Category>
  5. <Category title="电影" :listData="films"></Category>
  6. </div>
  7. </template>
  8. <script>
  9. import Category from './components/Category.vue';
  10. export default{
  11. name:"App",
  12. data(){
  13. return{
  14. foods:['火锅','烧烤','小龙虾','牛排'],
  15. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
  16. films:['《教父》','《拆弹专家》','《你好,李焕英》']
  17. }
  18. },
  19. components: {
  20. Category
  21. }
  22. }
  23. </script>
  24. <style lang="css">
  25. .container{
  26. /* 弹性布局 */
  27. display: flex;
  28. /* */
  29. justify-content: space-around;
  30. }
  31. </style>

Category.vue

  1. <template>
  2. <div class="category">
  3. <h3>{
  4. {title}}分类</h3>
  5. <ul>
  6. <li v-for="(item,index) in listData" :key="index">{
  7. {item}}</li>
  8. </ul>
  9. </div>
  10. </template>
  11. <script>
  12. export default{
  13. name:'Category',
  14. props:['listData','title']
  15. }
  16. </script>
  17. <style scoped>
  18. .category{
  19. background-color:skyblue ;
  20. width:200px;
  21. height:300px;
  22. }
  23. h3{
  24. text-align:center;
  25. background-color: orange;
  26. }
  27. </style>

二、默认插槽

69c37ddc4a994b198ffbbe84206f9147.png

App.vue

  1. <template>
  2. <div class="container">
  3. <Category title="美食" >
  4. <img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
  5. </Category>
  6. <Category title="游戏" >
  7. <ul>
  8. <li v-for="(g,index) in games" :key="index">{
  9. {g}}</li>
  10. </ul>
  11. </Category>
  12. <Category title="电影">
  13. <!-- controls 就可以播放了 -->
  14. <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
  15. </Category>
  16. </div>
  17. </template>
  18. <script>
  19. import Category from './components/Category.vue';
  20. export default{
  21. name:"App",
  22. data(){
  23. return{
  24. foods:['火锅','烧烤','小龙虾','牛排'],
  25. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
  26. films:['《教父》','《拆弹专家》','《你好,李焕英》']
  27. }
  28. },
  29. components: {
  30. Category
  31. }
  32. }
  33. </script>
  34. <style lang="css">
  35. .container{
  36. /* 弹性布局 */
  37. display: flex;
  38. /* */
  39. justify-content: space-around;
  40. }
  41. </style>

#

Category.vue

  1. <template>
  2. <div class="category">
  3. <h3>{
  4. {title}}分类</h3>
  5. <!-- 插槽 挖个坑,等着别人来这里 -->
  6. <slot>我是默认值,当使用者没有传递具体结构时,我会出现</slot>
  7. </div>
  8. </template>
  9. <script>
  10. export default{
  11. name:'Category',
  12. props:['title']
  13. }
  14. </script>
  15. <style scoped>
  16. video{
  17. width: 100%;
  18. }
  19. img{
  20. width: 100%;
  21. }
  22. .category{
  23. background-color:skyblue ;
  24. width:200px;
  25. height:300px;
  26. }
  27. h3{
  28. text-align:center;
  29. background-color: orange;
  30. }
  31. </style>

三、具名插槽

f8173521ab3641c7a463f9732c8a2cc5.png

App.vue

  1. <template>
  2. <div class="container">
  3. <Category title="美食" >
  4. <img slot="center" src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
  5. <a slot="footer" href="http://www.atguigu.com">更多美食</a>
  6. </Category>
  7. <Category title="游戏" >
  8. <ul slot="center">
  9. <li v-for="(g,index) in games" :key="index">{
  10. {g}}</li>
  11. </ul>
  12. <div class="foot" slot="footer">
  13. <a href="http://www.atguigu.com">单机游戏</a>
  14. <a href="http://www.atguigu.com">网络游戏</a>
  15. </div>
  16. </Category>
  17. <Category title="电影">
  18. <!-- controls 就可以播放了 -->
  19. <video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
  20. <div class="foot" slot="footer">
  21. <a href="http://www.atguigu.com">经典</a>
  22. <a href="http://www.atguigu.com">热门</a>
  23. <a href="http://www.atguigu.com">推荐</a>
  24. </div>
  25. </Category>
  26. </div>
  27. </template>
  28. <script>
  29. import Category from './components/Category.vue';
  30. export default{
  31. name:"App",
  32. data(){
  33. return{
  34. foods:['火锅','烧烤','小龙虾','牛排'],
  35. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
  36. films:['《教父》','《拆弹专家》','《你好,李焕英》']
  37. }
  38. },
  39. components: {
  40. Category
  41. }
  42. }
  43. </script>
  44. <style lang="css">
  45. .container,.foot{
  46. /* 弹性布局 */
  47. display: flex;
  48. /* */
  49. justify-content: space-around;
  50. }
  51. </style>

#

Category.vue

  1. <template>
  2. <div class="category">
  3. <h3>{
  4. {title}}分类</h3>
  5. <!-- 插槽 挖个坑,等着别人来这里 -->
  6. <slot name="center">我是默认值,当使用者没有传递具体结构时,我会出现</slot>
  7. <slot name="footer">我是默认值,当使用者没有传递具体结构时,我会出现</slot>
  8. </div>
  9. </template>
  10. <script>
  11. export default{
  12. name:'Category',
  13. props:['title']
  14. }
  15. </script>
  16. <style scoped>
  17. video{
  18. width: 100%;
  19. }
  20. img{
  21. width: 100%;
  22. }
  23. .category{
  24. background-color:skyblue ;
  25. width:200px;
  26. height:300px;
  27. }
  28. h3{
  29. text-align:center;
  30. background-color: orange;
  31. }
  32. </style>

四、作用域插槽

62d24bfac7c447c88fdaf2a3bf5683d4.png

App.vue

  1. <template>
  2. <div class="container">
  3. <Category title="游戏" >
  4. <!-- 这个时候template必须要用 要把它包裹起来才可以收到Category组件发过来的内容 -->
  5. <!-- scope 这个时候没有 d scope="games"把收过来的数据命名为games-->
  6. <template scope="games">
  7. <ul slot="center">
  8. <li v-for="(g,index) in games.games" :key="index">{
  9. {g}}</li>
  10. </ul>
  11. </template>
  12. </Category>
  13. <Category title="游戏" >
  14. <template scope="games">
  15. <ol slot="center">
  16. <li v-for="(g,index) in games.games" :key="index">{
  17. {g}}</li>
  18. </ol>
  19. </template>
  20. </Category>
  21. <Category title="游戏" >
  22. <template scope="{games}">
  23. <!-- es6写法 这样写之后 ,下面就不用写games. 了 -->
  24. <h4 v-for="(g,index) in games" :key="index">{
  25. {g}}</h4>
  26. </template>
  27. </Category>
  28. </div>
  29. </template>
  30. <script>
  31. import Category from './components/Category.vue';
  32. export default{
  33. name:"App",
  34. components: {
  35. Category
  36. }
  37. }
  38. </script>
  39. <style lang="css">
  40. .container,.foot{
  41. /* 弹性布局 */
  42. display: flex;
  43. /* */
  44. justify-content: space-around;
  45. }
  46. </style>

Category.vue

  1. <template>
  2. <div class="category">
  3. <h3>{
  4. {title}}分类</h3>
  5. <!-- 插槽 挖个坑,等着别人来这里 -->
  6. <!-- 谁往这个插槽中放东西,这个:games="games" msg="hello"就传给谁
  7. 这个地方是可以传递多个值的 -->
  8. <slot :games="games" msg="hello">我是默认值,当使用者没有传递具体结构时,我会出现</slot>
  9. </div>
  10. </template>
  11. <script>
  12. export default{
  13. name:'Category',
  14. props:['title'],
  15. data(){
  16. return{
  17. games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
  18. }
  19. },
  20. }
  21. </script>
  22. <style scoped>
  23. video{
  24. width: 100%;
  25. }
  26. img{
  27. width: 100%;
  28. }
  29. .category{
  30. background-color:skyblue ;
  31. width:200px;
  32. height:300px;
  33. }
  34. h3{
  35. text-align:center;
  36. background-color: orange;
  37. }
  38. </style>

发表评论

表情:
评论列表 (有 0 条评论,238人围观)

还没有评论,来说两句吧...

相关阅读