经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
Vue 插槽之插槽内容学习总结
来源:cnblogs  作者:授客  时间:2021/3/24 8:45:55  对本文有异议

插槽内容使用方法介绍

父组件中引用支持插槽内容的子组件,形如以下(假设子组件为NavigationLink.vue)

  1. <navigation-link url="/profile">
  2. Your Profile
  3. </navigation-link>

然后在子组件<template> 模板中使用<slot></slot>,形如以下:

  1. <a
  2. v-bind:href="url"
  3. class="nav-link"
  4. >
  5. <slot></slot>
  6. </a>

这样以后,当组件渲染的时候,子组件中的<slot></slot> 将会被替换为父组件模板中,子组件起始标签和结束标签之间的内容--这里称之为“插槽内容”。

插槽内可以包含任何模板代码,包括 HTML:

  1. <navigation-link url="/profile">
  2. <!-- 添加一个 Font Awesome 图标 -->
  3. <span class="fa fa-user"></span>
  4. Your Profile
  5. </navigation-link>

甚至其它的组件:

  1. <navigation-link url="/profile">
  2. <!-- 添加一个图标的组件 -->
  3. <font-awesome-icon name="user"></font-awesome-icon>
  4. Your Profile
  5. </navigation-link>

如果子组件 template没有包含一个 <slot> 元素,则父组件中,该组件起始标签和结束标签之间的任何内容都会被抛弃

应用举例

需求描述

自定义卡片组件,用于展示不同的内容,形式为 显示卡片标题和内容,卡片和卡片之间看起来需要有“分界条”

Testpage.vue

  1. <template>
  2. <div class="page-main">
  3. <div class="main-content">
  4. <card class="authors-single" title="测试标签1">
  5. <div style="height:50px;width:60px">hello</div>
  6. </card>
  7. <card class="authors-single" title="测试标签2">
  8. <div>卡片内容</div>
  9. </card>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import Card from "@/components/Card";
  15. export default {
  16. components: { Card },
  17. };
  18. </script>
  19. <style scoped lang="scss">
  20. .page-main {
  21. height: calc(100vh - 129px);
  22. padding: 10px 10px;
  23. display: flex;
  24. flex-direction: column;
  25. .main-content {
  26. overflow: auto;
  27. flex: auto;
  28. }
  29. }
  30. </style>

Card.vue

组件路径位于@/components/Card/Card.vue

  1. <template>
  2. <div class="card">
  3. <div class="card-title">{{title}}</div>
  4. <div class="card-content">
  5. <slot></slot>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. title: {
  13. type: String
  14. }
  15. }
  16. }
  17. </script>
  18. <style lang="scss" scoped>
  19. .card {
  20. display: flex;
  21. flex-direction: column;
  22. padding: 2px 5px;
  23. &-title {
  24. flex: none;
  25. padding: 0.4em 8px;
  26. font-size: 17px;
  27. position: relative;
  28. background-color: #f8f8f8;
  29. &::before {
  30. content: "";
  31. width: 4px;
  32. height: 100%;
  33. background: #59bcc7;
  34. position: absolute;
  35. top: 0;
  36. left: 0;
  37. }
  38. }
  39. &-content {
  40. flex: auto;
  41. padding: 10px;
  42. margin-top: 10px;
  43. background-color: #f8f8f8;
  44. }
  45. }
  46. </style>

效果

参考连接

https://cn.vuejs.org/v2/guide/components-slots.html#插槽内容

原文链接:http://www.cnblogs.com/shouke/p/14527866.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号