vue中的插槽slot
插槽(slot):是组件的一块HTML模板,父组件决定这块模板显不显示以及怎么显示。
位置由子组件自身决定(slot现在组件template的什么位置,父组件传过来的模板将来就显示在什么位置)
匿名插槽:只能有一个,可以放在组件的任何位置
<div class="father">
//在父组件在里面写了html模块
<son>
//子组件的匿名插槽被下面的div模板使用了
<div> <span>菜单1</span> </div> </son> </div> <template> <div class="son"> <slot></slot> </div> </template>
具名插槽:有name属性 可以在一个组件中多次出现,出现在不同的位置。
父组件:
<template>
<div class="father">
<h3>这里是父组件</h3>
<child>
<div class="tmpl" slot="up">
<span>菜单1</span>
<span>菜单2</span>
<span>菜单3</span>
<span>菜单4</span>
<span>菜单5</span>
<span>菜单6</span>
</div>
<div class="tmpl" slot="down">
<span>菜单-1</span>
<span>菜单-2</span>
<span>菜单-3</span>
<span>菜单-4</span>
<span>菜单-5</span>
<span>菜单-6</span>
</div>
//没有slot属性的html模板默认关联匿名插槽
<div class="tmpl">
<span>菜单->1</span>
<span>菜单->2</span>
<span>菜单->3</span>
<span>菜单->4</span>
<span>菜单->5</span>
<span>菜单->6</span>
</div>
</child>
</div>
</template>
子组件:
<template>
<div class="child">
// 具名插槽
<slot name="up"></slot>
<h3>这里是子组件</h3>
// 具名插槽
<slot name="down"></slot>
// 匿名插槽
<slot></slot>
</div>
</template>
作用域插槽(带数据插槽):在slot上面绑定数据(匿名插槽和具名插槽的的样式和内容都由父组件提供的模板决定)
父组件:
<template>
<div class="father">
<h3>这里是父组件</h3>
<!--第一次使用:用flex展示数据-->
<child>
<template slot-scope="user">
<div class="tmpl">
<span v-for="item in user.data">{{item}}</span>
</div>
</template> </child> <!--第二次使用:用列表展示数据-->
<child>
<template slot-scope="user">
<ul>
<li v-for="item in user.data">{{item}}</li>
</ul>
</template> </child> <!--第三次使用:直接显示数据-->
<child>
<template slot-scope="user">
{{user.data}}
</template> </child> <!--第四次使用:不使用其提供的数据, 作用域插槽退变成匿名插槽-->
<child>
我就是模板
</child>
</div>
</template>
子组件:
<template>
<div class="child">
<h3>这里是子组件</h3>
// 作用域插槽
<slot :data="data"></slot>
</div>
</template>
export default {
data: function(){
return {
data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba']
}
}
}
vue中的插槽slot的更多相关文章
- 在vue中使用插槽 slot
插槽(slot)这个概念非常重要 插槽的使用场景1:在子组件里面显示父组件的dom <div id='root'> <child content = '<p>Dell&l ...
- vue中的插槽slot理解
本篇文章参考赛冷思的个人博客 1.函数默认传参 在我们写js函数我们的可能会给他们一个默认的参数,写法是 function show(age,name){ var age = age || 20; v ...
- Vue中的插槽---slot
一:什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定将所携带的内容,插入到指定的某个位置,从而使模板分块,具有模块化的特质和更大的重用性. 插槽显不显示.怎样显示是由 ...
- vue中的插槽(slot)
vue中的插槽,指的是子组件中提供给父组件使用的一个占位符,用<slot></slot>标签表示,父组件可以在这个占位符中填充任何模板代码,比如HTML.组件等,填充的内容会替 ...
- vue中具名插槽的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 详解Vue中的插槽
作者: 小土豆 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/2436173500265335 什么是插槽 在日常的项目 ...
- 第八十九篇:Vue 重学插槽slot
好家伙, 1.什么是插槽? 插槽是vue为组件的封装者提供的能力.允许开发者在封装组件时, 把不确定的,希望由用户指定的部分定义为插槽 我们依然可以把它理解为一个占位符 1.1.插槽的基本用法 试 ...
- vue中$refs、$slot、$nextTick相关的语法
Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来 1.$data和$el var data = { a: 1 } var vm = new Vue({ el ...
- vue中的插槽
匿名插槽 // comp1 <div> <slot></slot> </div> // parent <comp>hello</com ...
随机推荐
- 【中间件】Redis 实战之主从复制、高可用、分布式
目录 简介 持久化 主从复制 高可用 Redis-Sentinel .NET Core开发 分布式 Redis-Cluster 配置说明 常见问题 简介 本节内容基于 CentOS 7.4.1708, ...
- 如何有效的报告bug?
对于比较棘手的bug,反馈务须清晰.详细.精确,我们给出以下6个建议: 1.现场演示:重复bug出现的操作步骤.这个适用于公司内部人员. 2.详细描述:在什么系统使用哪个版本的YoMail,做了什 ...
- 英特尔帮助优化 Epic 的《堡垒之夜》* 和 Unreal Engine*
您可能知道,Epic 的游戏<堡垒之夜>是 Unreal Engine* 技术的绝佳示例,<堡垒之夜>的开发团队正不断改进游戏,增加支持平台的数量并将信息反馈给引擎.为此,英特 ...
- unity2D背景移动补偿从而获得3d错觉效果
2d平台跳跃游戏当相机移动的时候背景跟随进行微调移动,从而使得玩家获得3d的错觉 using System.Collections;using System.Collections.Generic;u ...
- 【翻译】Brewer's CAP Theorem CAP定理
Brewer's CAP Theorem 原文地址:http://www.julianbrowne.com/article/brewers-cap-theorem Brewer’s (CAP) The ...
- 通过拓展Function.prototype实现一个AOP
AOP(面向切面的编程)主要是将一些与核心业务逻辑模块无关的功能抽离出来,这些功能通常包括日志统计,安全控制,或者是异常处理等等. 我们要做的就是拓展Function.prototype来“动态植入” ...
- MongoDB作为Windows服务来安装 错误1053:服务没有及时响应启动或控制请求
这个问题我解决了一晚上,用尽了所有百度 博客上的方法,都是失败的 结果重新换了一种安装的方法 视频讲解 非常清楚 https://www.bilibili.com/video/av31240330? ...
- error: Build input file cannot be found: '*******/node_modules/react-native/Libraries/WebSocket/libfishhook.a' 问题解决记录
解决了刚才的'config.h' file not found问题,本以为就可以顺畅的跑起来,谁知道又被恶心到了,Build input file cannot be found!!! 问题: err ...
- kafka学习总结之kafka简介
kafka是一个分布式,基于subscribe-publish的消息系统 特性:高吞吐量.低延迟.可扩展性.持久性(消息持久化到本地磁盘).可靠性.容错性(n个副本,允许n-1个节点失败).高并发(支 ...
- SQL Server Collation解惑
某些产品会有固定的DB Collation,如果提前创建DB的时候没有按照要求指定对应的Collation,这个时候就会报错,提示你Collation不匹配.在安装SQL Server的时候有时候需要 ...