Vue 插槽之插槽内容学习总结
插槽内容使用方法介绍
父组件中引用支持插槽内容的子组件,形如以下(假设子组件为NavigationLink.vue)
<navigation-link url="/profile">
Your Profile
</navigation-link>
然后在子组件<template> 模板中使用<slot></slot>,形如以下:
<a
v-bind:href="url"
class="nav-link"
>
<slot></slot>
</a>
这样以后,当组件渲染的时候,子组件中的<slot></slot> 将会被替换为父组件模板中,子组件起始标签和结束标签之间的内容--这里称之为“插槽内容”。
插槽内可以包含任何模板代码,包括 HTML:
<navigation-link url="/profile">
<!-- 添加一个 Font Awesome 图标 -->
<span class="fa fa-user"></span>
Your Profile
</navigation-link>
甚至其它的组件:
<navigation-link url="/profile">
<!-- 添加一个图标的组件 -->
<font-awesome-icon name="user"></font-awesome-icon>
Your Profile
</navigation-link>
如果子组件 template 中没有包含一个 <slot> 元素,则父组件中,该组件起始标签和结束标签之间的任何内容都会被抛弃
应用举例
需求描述
自定义卡片组件,用于展示不同的内容,形式为 显示卡片标题和内容,卡片和卡片之间看起来需要有“分界条”
Testpage.vue
<template>
<div class="page-main">
<div class="main-content">
<card class="authors-single" title="测试标签1">
<div style="height:50px;width:60px">hello</div>
</card>
<card class="authors-single" title="测试标签2">
<div>卡片内容</div>
</card>
</div>
</div>
</template>
<script>
import Card from "@/components/Card";
export default {
components: { Card },
};
</script>
<style scoped lang="scss">
.page-main {
height: calc(100vh - 129px);
padding: 10px 10px;
display: flex;
flex-direction: column;
.main-content {
overflow: auto;
flex: auto;
}
}
</style>
Card.vue
组件路径位于@/components/Card/Card.vue
<template>
<div class="card">
<div class="card-title">{{title}}</div>
<div class="card-content">
<slot></slot>
</div>
</div>
</template>
<script>
export default {
props: {
title: {
type: String
}
}
}
</script>
<style lang="scss" scoped>
.card {
display: flex;
flex-direction: column;
padding: 2px 5px;
&-title {
flex: none;
padding: 0.4em 8px;
font-size: 17px;
position: relative;
background-color: #f8f8f8;
&::before {
content: "";
width: 4px;
height: 100%;
background: #59bcc7;
position: absolute;
top: 0;
left: 0;
}
}
&-content {
flex: auto;
padding: 10px;
margin-top: 10px;
background-color: #f8f8f8;
}
}
</style>
效果

参考连接
https://cn.vuejs.org/v2/guide/components-slots.html#插槽内容
Vue 插槽之插槽内容学习总结的更多相关文章
- vue 使用Slot 分发内容 学习总结。
https://cn.vuejs.org/v2/guide/components.html#使用-Slot-分发内容 官方API地址 我对solt的理解是当组件中某一项需要单独定义,那么就应该使 ...
- Vue结合slot插槽分发父组件内容实现高度复用、更加灵活的dialog组件
之前写过一篇关于vue实现dialog会话框组件的文章(http://www.cnblogs.com/fozero/p/8546883.html)[http://www.cnblogs.com/foz ...
- 使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件
写在前面 之前写过一篇关于vue实现dialog会话框组件的文章http://www.cnblogs.com/fozero/p/8546883.html, 讲到了如何实现一个vue对话框组件,其中涉及 ...
- Vue组件-使用插槽分发内容
在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer></app-fo ...
- vue组件-使用插槽分发内容(slot)
slot--使用插槽分发内容(位置.槽口:作用: 占个位置) 官网API: https://cn.vuejs.org/v2/guide/components.html#使用插槽分发内容 使用组件时,有 ...
- Vue基础-匿名插槽与作用域插槽的合并和覆盖行为
Vue 测试版本:Vue.js v2.5.13 Vue 文档: <slot> 元素可以用一个特殊的特性 name 来进一步配置如何分发内容.多个插槽可以有不同的名字.具名插槽将匹配内容片段 ...
- Vue中的插槽---slot
一:什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定将所携带的内容,插入到指定的某个位置,从而使模板分块,具有模块化的特质和更大的重用性. 插槽显不显示.怎样显示是由 ...
- Vue 之 slot(插槽)
前言: vue中关于插槽的文档说明很短,语言又写的很凝练,再加上其和methods,data,computed等常用选项在使用频率.使用先后上的差别,这就有可能造成初次接触插槽的开发者容易产生“算了吧 ...
- vue中的插槽(slot)
vue中的插槽,指的是子组件中提供给父组件使用的一个占位符,用<slot></slot>标签表示,父组件可以在这个占位符中填充任何模板代码,比如HTML.组件等,填充的内容会替 ...
- 详解Vue中的插槽
作者: 小土豆 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/2436173500265335 什么是插槽 在日常的项目 ...
随机推荐
- Linux的IO调度程序
Linux的IO调度机制浅析 现代计算机体系中,磁盘的速度和CPU的速度差距太大了,如果简单的将系统的IO请求按照请求的顺序进行顺序处理的话,系统的IO开销将导致系统的效率十分的低下,因此就需要将IO ...
- windows10 浏览器跑分对比!
2015-12-12 windows10 浏览器跑分对比! YOUR BROWSER SCORES! MaxScore=555http://html5test.com/i ...
- Get your site working on Google Search Console , 在 Google Search Console中运行您的网站, Google Search Console
1 1 https://support.google.com/webmasters/topic/4564315? Search Console Help SEARCH CONSOLEHELP FORU ...
- LeetCode 算法面试题汇总
LeetCode 算法面试题汇总 算法面试题 https://leetcode-cn.com/problemset/algorithms/ https://leetcode-cn.com/proble ...
- DLL & Dynamic-link library
DLL & Dynamic-link library 动态链接库 .dll 动态链接库(英语:Dynamic-link library,缩写为 DLL)是微软公司在微软视窗操作系统中实现共享函 ...
- shit LeetCode interview Question
shit LeetCode interview Question https://leetcode.com/interview/1/ 有点晕,啥意思,没太明白,到底是要按什么排序呀? 去掉 标识符 不 ...
- HDFS 01 - HDFS是什么?它的适用场景有哪些?它的架构是什么?
目录 1.HDFS 是什么 1.1 简单介绍 1.2 发展历史 2.HDFS 应用场景 2.1 适合的应用场景 2.2 不适合的应用场景 3.HDFS 的架构 4.NameNode 和 DataNod ...
- HTTPS原理解析
HTTPS 一些概念 http 概述 HTTP是一个客户端(用户)和服务端(网站)之间请求和应答的标准,通常使用TCP协议.其本身位于TCP/IP协议族的应用层. 特点 - 客户端&服务器 - ...
- Java进阶专题(二十六) 数据库原理研究与优化
前言 在一个大数据量的系统中,这些数据的存储.处理.搜索是一个非常棘手的问题. 比如存储问题:单台服务器的存储能力及数据处理能力都是有限的, 因此需要增加服务器, 搭建集群来存储海量数据. 读写性能问 ...
- 开源OA办公平台功能介绍:应用市场-固定资产管理(一)功能设计
概述 应用市场-固定资产管理,是用来维护管理企业固定资产的一个功能.其整个功能包括对固定资产的台账信息.领用.调拨.借用.维修.盘点.报废等一整个生命周期的动态管理过程.力求客户安装就可以使用. 本应 ...