slot 插槽

  1. 插槽内容
const component ={
template: `
<div>
<slot></slot>
</div>
`
} new Vue ({
components: {
com: components
},
el: '#app',
template: `
<div>
<com>
this is contnent.
</com>
</div>
`
})
  1. 具名插槽
const component = {
template: `
<div>
<slot name="header"></slot>
<slot name="main"></slot>
<slot name="footer"></slot>
</div>
`
} new Vue({
components: {
component
},
el: '#app',
template: `
<div>
<com>
<template name="header">header</template>
<template name="main">main</template>
<template name="footer">footer</template>
</com>
</div>
`
})

根据是否有插槽内容,来判断显示内容。vue提供$slots属性,每个具名插槽的name都是$slots的属性,但是有的直接使用<slot></slot>没有具名,则用$slots.default表示,如下例子:

const component = {
template: `
<div :style="style">
<slot name="header"></slot>
<slot v-if="$slots.default"></slot>
<template v-if="!$slots.default">
there are no data.
</template>
<slot name="footer" v-if="$slots.footer"></slot>
<template v-if="!$slots.footer">
if footer is not exsit, show the content.
</template>
</div>
`,
data () {
return {
style: {
width: '200px',
height: '200px',
border: '1px solid #aaa',
}
}
}
} new Vue({
components: {
ComOne: component
},
el: '#app',
template: `
<div>
<ComOne>
<template slot="header">header</template>
</ComOne>
</div>
`
});
  1. slot-scope的用法

将子组件的属性通过 slot-scope 传递给父组件中对应子组件插槽内容中。

const component = {
template: `
<div :style="style">
<slot :value="value"></slot>
</div>
`,
props: {
value: {
default: 'liangcheng',
type: String
},
},
data () {
return {
style: {
width: '200px',
height: '200px',
border: '1px solid #aaa',
}
}
}
} new Vue({
components: {
ComOne: component
},
el: '#app',
template: `
<div>
<ComOne value="lc">
<template slot-scope="props">{{ props.value }}</template>
</ComOne>
</div>
`
});

1_02 Vue Slot的更多相关文章

  1. vue自学入门-4(vue slot)

    vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...

  2. vue slot nested bug

    vue slot nested bug slot name bug Error <slot name="global-system-guide-slot"></s ...

  3. Vue slot 插槽用法:自定义列表组件

    Vue 框架的插槽(slot)功能相对于常用的 v-for, v-if 等指令使用频率少得多,但在实现可复用的自定义组件时十分有用.例如,如果经常使用前端组件库的话,就会经常看到类似的用法: < ...

  4. Vue slot插槽

    插槽用于内容分发,存在于子组件之中. 插槽作用域 父级组件作用域为父级,子级组件作用域为子级,在哪定义的作用域就在哪. 子组件之间的内容是在父级作用域的,无法直接访问子组件里面的数据. 插槽元素 &l ...

  5. Vue slot插槽内容分发

    slot插槽使用 使用场景,一般父组件中又一大段模板内容需要运用到子组件上.或者更加复杂的,子组件需要运用到父组件大段模板内容,而子组件却不知道挂载的内容是什么.挂载点的内容是由父组件来决定的. Sl ...

  6. vue slot 复用

    话不投机半句多,直接上代码 有3步 第一步:创建渲染slot的组件 重要 第二步:为slot添加父组件数据(props) 重要 第三步:使用 第一步:创建渲染slot的组件 首选创建一个单文件组价,由 ...

  7. vue slot

    一直觉得vue的slot比较申请,而且比较深奥,总有点不想用的感觉,事实上,在一定程度上,也真的可以完全避开slot就能把一个项目完全搭建完成. 但是随着用的次数越来越多,看到的内容也越来越多的情况, ...

  8. vue slot插槽的使用方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Vue slot简单理解

    情形一: 子组件定义了具名的slot,父组件使用具名的slot,slot显示顺序为子组件定义slot的顺序 子组件: Vue.component('child',{ template:`<div ...

随机推荐

  1. Linux下apache activemq的安装与配置

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范 的 JMS Provider实现,尽管JMS规范出台已经是很 ...

  2. Android 5.0 Phone初始化分析

    已经更新至个人blog:http://dxjia.cn/2015/07/android-5-0-phone-init-analysis/ persistent属性 要想了解phone的框架,首先需要了 ...

  3. java-信息安全(十三)-数字签名,代码签名【Java证书体系实现】

    概述 信息安全基本概念 前置 java-信息安全(十二)-数字签名[Java证书体系实现] 过程 通过工具JarSigner可以完成代码签名.  这里我们对tools.jar做代码签名,命令如下: 进 ...

  4. linux 如何快速的查找日志中你所要查找的信息

    在工作中我总会通过日志来查找相关问题,但有时候日志太多有不知道又不知道日志什么时候打印的,这时我们可以通过一下方法来查找: 1.把目录跳到你日志文件存放的地方 2.grep  关键字  *    例如 ...

  5. 安卓开发笔记——Notification通知栏

    当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标.提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯.Android给我们提供了NotificationManager ...

  6. README.md文件编辑

    111 蜗牛图片管理系统 =============== ## 安装环境 >+ thinkphp版本: >+ php版本:.0以上 >+ 需要安装在服务器环境,免费使用 ### 特点 ...

  7. eclipse安装emmet插件

    http://www.cnblogs.com/matchless/archive/2013/04/10/3011973.html

  8. SpringBoot------Servlet3.0的注解自定义原生Listener监听器

    前言 常用监听器: //contextListener可以监听数据库的连接,第三方组件的交互,还有静态文件加载等等 servletContextListener HttpSessionListener ...

  9. How to hard reset Visual Studio instance

    When developing extensions sometimes you just mess up, others someone else does. If you start gettin ...

  10. 在springBoot中配置web.xml中配置的servlet

    第一种 web.xml (截取的需要转换的) 当拦截到 /socke t时执行该servlet <servlet> <servlet-name>websocket</se ...