1. 什么是插槽

  插槽slot 是往父组件中插入额外内容,实现组件的复用,一个插槽插入到一个对应的标签中

2. 实例:

  一个组件中不允许有两个匿名插槽

</head>
<body>
<div id="root">
<child>
<div class="header" slot="header">header</div>
<div class="footer" slot="footer">footer</div>
hello <!-- 没有名字的标签默认会放置到name:default的slot中 -->
</child>
</div>
<script>
Vue.component('child',{
template: `<div>
<slot name="header"></slot> <!-- 有名插槽 -->
<div class="content">content</div>
<slot name="footer"></slot> <!-- 有名插槽 -->
<slot>如果没有hello就显示slot的内容</slot> <!-- 匿名插槽 slot中可以设置默认内容,如果传递了内容则替换掉 -->
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>

3. 作用域插槽

固定写法:父组件中<template slot-scope="props"></template>      //props可以自定义用来接收子组件的数据

<body>
<div id="root">
<child>
<template slot-scope="props">
<li>{{props.item2}}</li> //接收子组件的item值
</template>
</child>
</div>
<script>
Vue.component('child',{
data(){
return{
list:[1,2,3,4]
}
},
template: `<div>
<ul>
<slot v-for="item of list" :item2="item"></slot>
</ul>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>

Vue插槽 slot的更多相关文章

  1. vue 插槽slot

    本文是对官网内容的整理 https://cn.vuejs.org/v2/guide/components.html#编译作用域 在使用组件时,我们常常要像这样组合它们: <app> < ...

  2. 三、深入Vue组件——Vue插槽slot、动态组件

    一.插槽slot() 1.1简单插槽slot [功能]用于从父组件中,通过子组件写成双标签,向子组件中放入自定的内容 parent.vue [1]首先把child写成双标签样式,把要插入的内容放双标签 ...

  3. vue 插槽slot总结 slot看这篇就够了

    一直模糊所以梳理一下,看了好多篇园友的文章和官网文档在这整理一下 默认插槽 //slot组件<template> <div class="slots"> s ...

  4. vue插槽slot的理解与使用

    一.个人理解及插槽的使用场景 刚开始看教程我的疑惑是为什么要用插槽,它的使用场景是什么,很多解释都是“父组件向子组件传递dom时会用到插槽”,这并不能很好的解决我的疑惑.既然你用了子组件,你为什么要给 ...

  5. vue 插槽 slot

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. vue 插槽 ------ slot 简单理解

    solt 插槽 内容分发 什么是插槽 Vue 实现了一套内容分发的 API,将 `` 元素作为承载分发内容的出口. 插槽显示的位置却由子组件自身决定,槽写在组件模板的什么位置,父组件传过来的模板将来就 ...

  7. Vue插槽slot理解与初体验 ~

    一.插槽的理解 1.官网介绍 Vue 实现了一套内容分发的 API,将 <slot> 元素作为承载分发内容的出口. 2.为什么使用插槽 Vue 中有一个重要的概念-组件,可以在开发中将子组 ...

  8. Vue 插槽 slot的简单实用

  9. Vue插槽:(2.6.0以后版本弃用slot和slot-scope,改用v-slot)

    关于Vue插槽的概念,大家可以从vue官网的api查看,我是看到网站的对于初接触 这个要概念的人来说不是很清楚,我来贴下原码,就比较直观了 贴下原码: 具名插槽:v-slot:header Html: ...

随机推荐

  1. Html5 突破微信限制实现大文件分割上传

    先来前端代码 <!DOCTYPE html> <html> <head> <meta name="viewport" content=&q ...

  2. PHP 运行环境和服务器相关配置

    1.在DOS命令窗口输入 mysql -hlocalhost -uroot -p回车 进入mysql数据库, 其中-h表示服务器名,localhost表示本地:-u为数据库用户名,root是mysql ...

  3. CCF201803-1 跳一跳

    试题编号: 201803-1 试题名称: 跳一跳 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱. 简化后的跳一跳规则如下: ...

  4. Laravel 开源电商体验与部署

    体验 开源项目已经部署了体验环境,开源通过扫描下方小程序码进行体验: 我们部署了 Laravel API demo 环境,访问地址:https://demo-open-admin.ibrand.cc/ ...

  5. JS支持可变参数(任意多个)

    <script type="text/javascript"> function abc(){ //js中有个变量arguments,可以访问所有传入的值 for(va ...

  6. vue+node+mongodb实现的功能

    用vue+node +mongodb实现前后台交互的页面代码,已经上传到github上, 地址是: https://github.com/GainLoss/vue-node-mongodb https ...

  7. 【转载】#443 - An Interface Cannot Contain Fields

    An interface can contain methods, properties, events or indexers. It cannot contain fields. interfac ...

  8. android 中组件继承关系图,一目了然

    View继承关系图 Adapter适配器继承关系图 Activity继承关系图

  9. HDU 5687 Problem C 【字典树删除】

    传..传送:http://acm.hdu.edu.cn/showproblem.php?pid=5687 Problem C Time Limit: 2000/1000 MS (Java/Others ...

  10. 最短路算法 —— Dijkstra算法

    用途: 解决单源最短路径问题(已固定一个起点,求它到其他所有点的最短路问题) 算法核心(广搜): (1)确定的与起点相邻的点的最短距离,再根据已确定最短距离的点更新其他与之相邻的点的最短距离. (2) ...