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 ...
随机推荐
- Bear + Reminders 是完美的Thing 3 的替代品
如今同类功能的APP在AppStore上呈现泛滥之势,尤其是时间管理.任务管理之类的APP.其中比较出名的就有“Things 3”这款APP,这是一款多年不更新,一更新就获奖的APP.目前在AppSt ...
- 使用Xilinx UART-LITE IP实现串口--逻辑代码实现
`timescale 1ns / 1ps /////////////////////////////////////////////////////////////////////////////// ...
- java web 下载本地文件并弹出下载框
window.open("/dept/download") jsp代码 @RequestMapping(value = "/download", method ...
- synchronized和Lock复习
刚学编程的时候,不懂得同步的概念,只认为程序按照自己写的顺序执行, 直到学到多线程,但当时理解同步问题,也只是面对临界资源需要加锁去控制, 解决一些,如生产消费的问题.但当时一直没考虑过,多线程的情况 ...
- LeetCode 242 Valid Anagram 解题报告
题目要求 Given two strings s and t , write a function to determine if t is an anagram of s. 题目分析及思路 给出两个 ...
- Java学习-051-Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError
错误信息:Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting St ...
- 【Mac】-NO.161.Mac.1 -【MacOS Error running 'Tomcat 8.5.371': Cannot run program Permission denied】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- Python基础之数组和向量化计算总结
一.多维数组 1.生成ndarray (array函数) .np.array()生成多维数组 例如:import numpy as npdata1=[6,7.5,8,0,1] #创建简 ...
- JAVA String中文乱码
System.out.println(str); String str1 = new String(str.getBytes("ISO-8859-1"), "utf-8& ...
- Respone弹窗
Response.Write("<script>window.open('default.aspx?iID=" + GridView1.DataKeys[GridVie ...