Vue 组件&组件之间的通信 之 使用slot分发内容
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分发内容的更多相关文章
- vue组件详解(四)——使用slot分发内容
一.什么是slot 在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer>& ...
- Vue:实践学习笔记(6)——使用SLOT分发内容
Vue:实践学习笔记(6)——使用SLOT分发内容 Slot Slot是什么 Slot是父子组件的通讯方式,可以将父组件的内容显示到子组件之中. 使用SLOT前 比如我在定义组件的时候,在里面输入了X ...
- Vue组件之props,$emit与$on以及slot分发
组件实例之间的作用域是孤立存在,要让它们之间的信息互通,就必须采用组件的通信方式 props用于父组件向子组件传达信息 1.静态方式 eg: <body> <div id=&quo ...
- vue组件详解——使用slot分发内容
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.什么是slot 在使用组件时,我们常常要像这样组合它们: <app& ...
- vue 使用Slot 分发内容 学习总结。
https://cn.vuejs.org/v2/guide/components.html#使用-Slot-分发内容 官方API地址 我对solt的理解是当组件中某一项需要单独定义,那么就应该使 ...
- Vue.js-----轻量高效的MVVM框架(十一、使用slot分发内容)
#单个slot html: <h3>#单个slot</h3> <!-- 定义child01模板 --> <template id="child01& ...
- vue 外卖app(3) 利用slot分发内容
1. 增加一个HeaderTop.vue <template> <header class="header"> <slot name="le ...
- vue组件父子之间相互通信案例
- 简述在Vue脚手架中,组件以及父子组件(非父子组件)之间的传值
1.组件的定义 组成: template:包裹HTML模板片段(反映了数据与最终呈现给用户视图之间的映射关系) 只支持单个template标签: 支持lang配置多种模板语法: script:配置Vu ...
随机推荐
- RxSwift 介绍
RxSwift 介绍 中文文档 https://beeth0ven.github.io/RxSwift-Chinese-Documentation/ https://medium.com/@DianQ ...
- Source Insight相关设置
#Source Insight中按快捷键在其他编辑器中打开当前文件 "D:\Program Files\Zend\ZendStudio-5.5.0\bin\ZDE.exe" %f ...
- LeetCode 217 Contains Duplicate 解题报告
题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...
- Cutting Codeforces Round #493 (Div. 2)
Cutting There are a lot of things which could be cut — trees, paper, “the rope”. In this problem you ...
- python中闭包的理解
闭包的三个条件: 1.函数(外函数)中定义了内函数:2.内函数使用了外函数的非全局变量:3.外函数最终返回的是内函数的引用. 简单闭包事例: #outerfunc为外函数 def outerfunc( ...
- 2017(5)软件架构设计,web系统的架构设计,数据库系统,分布式数据库
试题五(共 25 分) 阅读以下关于 Web 系统架构设计的叙述,在答题纸上回答问题1 至问题 3. [说明] 某公司开发的 B2C 商务平台因业务扩展,导致系统访问量不断增大,现有系统访问速度缓慢, ...
- Python文件常用操作方法
Python文件常用操作方法 一.对File对象常用操作方法: file= open(file, mode='r', buffering=-1, encoding=None, errors=None, ...
- 原生js标识当前导航位置(给当前导航一个className=active)
导航html结构为: <div class="header2-nav"> <a href="index.html">首页</a&g ...
- DS1-14
#include <stdio.h> #define MAXSIZE 10000 int MaxSubseqSum4(int List[], int N); int main() { in ...
- 内网gitlab11.2升级至11.4.5
当前gitlab版本 宿主机是一台ubuntu 运行备份命令 sudo gitlab-rake gitlab:backup:create STRATEGY=copy 升级命令 sudo apt-get ...