slot详细介绍网址:https://cn.vuejs.org/v2/api/#slot

有时候我们需要在自定义组件内书写一些内容,例如: <com-a> <h1>title</h1> </com-a> 如果想获取上面代码片段中h1标签的内容该怎么办呢?

Vue提供了一个极为方便的内置组件<slot>;

初始界面:

初始demo:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> 使用slot分发内容</title>
</head>
<body>
<div> <my-component-a></my-component-a>
</div>
</body>
<template id="template-a">
<div>
<h1>my-component-a</h1> <hr />
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script>
let comA = {
template : "#template-a" } new Vue({
data:{ },
components : {
"my-component-a" : comA } }).$mount('div');
</script>
</html>

slot放在那里,获取到的内容就放在那里:

可以根据其name属性进行排其位置:

定义属性name的demo

<div>

            <my-component-a>

                <h1 slot='title'>大标题</h1>
<ol slot='olli'>
<li>a</li>
<li>b</li>
<li>c</li> </ol>
<a href="#" slot='res'>点我</a>
</my-component-a>
</div>
</body>
<template id="template-a">
<div>
<slot name='title'></slot>
<h1>my-component-a</h1>
<slot name='olli'></slot>
<slot name='res'></slot> <hr />
</div>
</template>

使用slot分发内容总的demo:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> 使用slot分发内容</title>
</head>
<body>
<div> <my-component-a> <h1 slot='title'>大标题</h1>
<ol slot='olli'>
<li>a</li>
<li>b</li>
<li>c</li> </ol>
<a href="#" slot='res'>点我</a>
</my-component-a>
</div>
</body>
<template id="template-a">
<div>
<slot name='title'></slot>
<h1>my-component-a</h1>
<slot name='olli'></slot>
<slot name='res'></slot> <hr />
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script>
let comA = {
template : "#template-a" } new Vue({
data:{ },
components : {
"my-component-a" : comA } }).$mount('div');
</script>
</html>

使用slot分发内容总demo

Vue 组件&组件之间的通信 之 使用slot分发内容的更多相关文章

  1. vue组件详解(四)——使用slot分发内容

    一.什么是slot 在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer>& ...

  2. Vue:实践学习笔记(6)——使用SLOT分发内容

    Vue:实践学习笔记(6)——使用SLOT分发内容 Slot Slot是什么 Slot是父子组件的通讯方式,可以将父组件的内容显示到子组件之中. 使用SLOT前 比如我在定义组件的时候,在里面输入了X ...

  3. Vue组件之props,$emit与$on以及slot分发

    组件实例之间的作用域是孤立存在,要让它们之间的信息互通,就必须采用组件的通信方式  props用于父组件向子组件传达信息 1.静态方式 eg: <body> <div id=&quo ...

  4. vue组件详解——使用slot分发内容

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code     一.什么是slot 在使用组件时,我们常常要像这样组合它们: <app& ...

  5. vue 使用Slot 分发内容 学习总结。

    https://cn.vuejs.org/v2/guide/components.html#使用-Slot-分发内容    官方API地址 我对solt的理解是当组件中某一项需要单独定义,那么就应该使 ...

  6. Vue.js-----轻量高效的MVVM框架(十一、使用slot分发内容)

    #单个slot html: <h3>#单个slot</h3> <!-- 定义child01模板 --> <template id="child01& ...

  7. vue 外卖app(3) 利用slot分发内容

    1. 增加一个HeaderTop.vue <template> <header class="header"> <slot name="le ...

  8. vue组件父子之间相互通信案例

  9. 简述在Vue脚手架中,组件以及父子组件(非父子组件)之间的传值

    1.组件的定义 组成: template:包裹HTML模板片段(反映了数据与最终呈现给用户视图之间的映射关系) 只支持单个template标签: 支持lang配置多种模板语法: script:配置Vu ...

随机推荐

  1. Ubuntu mysql连接错误10060/10061的方法

    mysql 10060错误一般因为防火墙 ufw allow 3306; //允许外部访问3306端口ufw allow from 192.168.1.115;  //允许此IP访问所有的本机端口uf ...

  2. ul的margin撑不开想要的距离的办法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Jumpserver之快速入门

    一,系统设置 1.1基本设置 修改 url 的"localhost"为你的实际 url 地址, 否则邮件收到的地址将为"localhost" 也无法创建新用户 ...

  4. 微信小程序页面带参数跳转

    页面传递参数的方式 data-para js获取参数

  5. 如何在云服务器创建maven私有仓库

    参考链接:https://blog.csdn.net/silence_jjj/article/details/77531916 nexus3创建maven私有仓库(windows) 1.官网:http ...

  6. IP协议号 IP首部中有8位协议号,用于指明IP的上层协议.

    IP协议号   IP首部中有8位协议号,用于指明IP的上层协议. 0 HOPOPT IPv6 逐跳选项 1 ICMP Internet 控制消息 2 IGMP Internet 组管理 3 GGP 网 ...

  7. Express全系列教程之(六):cookie的使用

    一.关于Cookie 在我们关闭一个登录过的网址并重新打开它后,我们的登录信息依然没有丢失:当我们浏览了商品后历史记录里出现了我们点击过的商品:当我们推回到首页后,推荐商品也为我们选出了相似物品:事实 ...

  8. Java代码实现封装多级树结构对象

    前言: 在开发中,我们经常见到,前端展示树状结构的,这时候就需要后端去封装一个多级树结构对象,前端根据这样结构的数据去渲染数据,这篇文章讲的是如何封装成多级树结构对象. 正文: 1.先封装个树结构的对 ...

  9. 第 1 章 JS变量、作用域

    目录 一. 判断变量类型 二.作用域 和 上下文 1. 作用链 2. 上下文(this) 二.JS的解析机制 1. 预解析 三.垃圾收集 内存管理销毁 @(es5) 基本类型 引用类型 一. 判断变量 ...

  10. 运维自动化之系统部署 cobbler(三)

    cobbler 介绍 Cobbler: 快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat.Fedora.CentOS.Debian.Ubuntu和SuSE,也可以支持网 ...