Vue.js特性Scoped Slots的浅析


Posted in Javascript onFebruary 20, 2019

什么是scoped slots

A scoped slot is a special type of slot that functions as a reusable template (that can be passed data to) instead of already-rendered-elements.

上面是官方的定义。

作用域插槽(Scoped Slots)是vue.js中一个非常有用的特性,它可以使组件更加通用和复用。唯一的问题是理解起来比较困难。试图去让你理解父与子作用域的交织关系,像解决一道数学难题。

简单点说slot就是插槽,它是可以被替换掉的,替换它的内容是可以拿到当前组件的上下文的
(为了简单起见,以下例子以模板为主)

举个简单的例子

//button.vue
<template>
 <button class="btn">
  <slot></slot> //相当于是占位
 </button>
</template>

<script>
export default {

}
</script>
//app.vue
<template>
 <div id="app">
 <Button>Buton with text</Button>
 <Button>
 <i class="fa fa-copy"></i>//这里取代了slot的位置
 </Button>

 <Button>
 <i class="fa fa-user"></i>Profile
 </Button>

 </div>
</template>

<script>
 import Button from './components/Button.vue'
 export default {
 name: 'app',
 components: {
 Button
 }
 }
</script>

slot其实就是一个占位,button.vue的slot位置会被app.vue里面的替换了。

复杂例子1:slot内的东西可以获取父组件的上下文信息

//list.vue
<template>
 <div>
 <slot v-for="item in items"
   :item="item">//这里是slot的占位
 </slot> 
 </div>
</template>
//app.vue
<template>
 <div id="app">
 <List :items="listItems">
  <div slot-scope="row"
  class="list-item1">//这里可以获取到item,item原本是属于List组件内部的。也就是说slot获取了父组件的上下文。
  {{row.item.text}}
 </div>
 </List>
 </div>
</template>

解释见上面代码注释。注意一点的是slot-scope=”row” 这里的名字(row)是可以任意取的。

named slots

可以直接放到普通标签上面,可以放template标签上

slot里面的作用域是普通标签或者template是一致的。不能访问父组件的作用域。

复杂例子2:slot里面是可以放东西的,是默认的模板,可被替换。

//table.vue
<template>
 <table class="table">
 <thead>
  <slot name="columns">//这里定义了一个slot,名字叫columns,也就是说这里的内容是可以被替换掉的
  <th v-for="column in columns">
   {{column}}
  </th>
  </slot>
 </thead>
 <tbody>
 <tr v-for="item in data">
  <slot :row="item">//这里slot有一个prop是row
  <td v-for="column in columns"
   v-if="hasValue(item, column)">
   {{itemValue(item, column)}}
  </td>
  </slot>
 </tr>
 </tbody>
 </table>
</template>
//app.vue
<template>
 <div id="app">
 <CustomTable :data="tableData" 
   :columns="tableColumns">
 </CustomTable>

  <div class="table-separator"></div>

 <CustomTable :data="tableData">
  <template slot="columns">//这里有一个slot="columns",意思是替换table.vue里面名字叫columns的slot
  <th>Title</th>
  <th>
  <i class="fa fa-images"></i> Image
  </th>
  <th class="actions-row">
  <i class="fab fa-vuejs vue-icon"></i> Actions
  </th>
 </template>

 <template slot-scope="{row}">//这里替换table.vue里面slot为row的内部内容
 <td class="bold-row">
  {{row.title}}
 </td>
 <td class="img-row">
  <img :src="row.img">
 </td>
 <td class="actions-row">
  <Button @click.native="handleAction('Edit')">
  <i class="fa fa-edit"></i>
  </Button>
  <Button @click.native="handleAction('Delete')" type="danger">
  <i class="fa fa-trash"></i>
  </Button>
 </td>
 </template> 
 </CustomTable>
 </div>
</template>

Vue.js特性Scoped Slots的浅析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js控制表单奇偶行样式的简单方法
Jul 31 Javascript
js(jQuery)获取时间的方法及常用时间类搜集
Oct 23 Javascript
JavaScript获取XML数据附示例截图
Mar 05 Javascript
浅谈addEventListener和attachEvent的区别
Jul 14 Javascript
Node.js中防止错误导致的进程阻塞的方法
Aug 11 Javascript
self.attachevent is not a function的解决方法
Apr 04 Javascript
jQuery实现简单的抽奖游戏
May 05 jQuery
bootstrap table支持高度百分比的实例代码
Feb 28 Javascript
vue.js实现插入数值与表达式的方法分析
Jul 06 Javascript
vue-swiper的使用教程
Aug 30 Javascript
vue 使用 vue-pdf 实现pdf在线预览的示例代码
Apr 26 Javascript
JS前端轻量fabric.js系列物体基类
Aug 05 Javascript
微信小程序五子棋游戏的棋盘,重置,对弈实现方法【附demo源码下载】
Feb 20 #Javascript
详解关于element级联选择器数据回显问题
Feb 20 #Javascript
JavaScript ES2019中的8个新特性详解
Feb 20 #Javascript
echarts实现词云自定义形状的示例代码
Feb 20 #Javascript
JS拖拽排序插件Sortable.js用法实例分析
Feb 20 #Javascript
详解webpack 最简打包结果分析
Feb 20 #Javascript
jQuery表单元素过滤选择器用法实例分析
Feb 20 #jQuery
You might like
cache_lite试用
2007/02/14 PHP
php如何调用webservice应用介绍
2012/11/24 PHP
thinkphp autoload 命名空间自定义 namespace
2015/07/17 PHP
win平台安装配置Nginx+php+mysql 环境
2016/01/12 PHP
js 判断 enter 事件
2009/02/12 Javascript
用jQuery模拟select下拉框的简单示例代码
2014/01/26 Javascript
JS 在指定数组中随机取出N个不重复的数据
2014/06/10 Javascript
jQuery插件slick实现响应式移动端幻灯片图片切换特效
2015/04/12 Javascript
深入浅析react native es6语法
2015/12/09 Javascript
BootStrap 超链接变按钮的实现方法
2016/09/25 Javascript
JSON字符串和JSON对象相互转化实例详解
2017/01/05 Javascript
如何从零开始手写Koa2框架
2019/03/22 Javascript
微信小程序学习笔记之文件上传、下载操作图文详解
2019/03/29 Javascript
Vue插件之滑动验证码
2019/09/21 Javascript
jQuery实现弹幕特效
2019/11/29 jQuery
[02:16]DOTA2超级联赛专访Burning 逆袭需要抓住机会
2013/06/24 DOTA
Python数据结构与算法之二叉树结构定义与遍历方法详解
2017/12/12 Python
利用Python代码实现数据可视化的5种方法详解
2018/03/25 Python
Python实例方法、类方法、静态方法的区别与作用详解
2019/03/25 Python
Python 多线程,threading模块,创建子线程的两种方式示例
2019/09/29 Python
使用Python生成200个激活码的实现方法
2019/11/22 Python
Xadmin+rules实现多选行权限方式(级联效果)
2020/04/07 Python
pytorch中 gpu与gpu、gpu与cpu 在load时相互转化操作
2020/05/25 Python
python 星号(*)的多种用途
2020/09/21 Python
python 下载文件的几种方法汇总
2021/01/06 Python
是否有自动比较结构的方法
2015/06/03 面试题
工伤事故赔偿协议书
2014/04/15 职场文书
岗位安全生产责任书
2014/07/28 职场文书
大学生创业计划书
2014/08/14 职场文书
竞选班干部演讲稿300字
2014/08/20 职场文书
施工安全协议书范本
2014/09/26 职场文书
作风建设剖析材料
2014/10/06 职场文书
派出所正风肃纪剖析材料
2014/10/10 职场文书
值班管理制度范本
2015/08/06 职场文书
如何使用Maxwell实时同步mysql数据
2021/04/08 MySQL
python中pymysql包操作数据库方法
2022/04/19 Python