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. Kubernetes集群部署之一系统环境初始化

    安装版本: centos version: 7.4 docker version: 18.03.1-ce kubectl version: v1.10.1 etcdctl version: 3.2.1 ...

  2. 使用Redis List简单实现抢红包

    在这里不讨论抢红包的算法,只用redis简单尝试解决抢红包.借助redis单线程和List的POP方法. static void Main(string[] args) { IRedisHelper ...

  3. Markdown 标题

    用 Markdown 书写时,只需要在文本前面加上 # 即可创建标题,Markdown 支持六级标题,语法及效果如下 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五 ...

  4. OpenGL——三维多面体实现

    #include<iostream> #include <math.h> #include<Windows.h> #include <GL/glut.h> ...

  5. Qt编写守护程序保证程序一直运行(开源)

    没有任何人敢保证自己写的程序没有任何BUG,尤其是在商业项目中,程序量越大,复杂度越高,出错的概率越大,尤其是现场环境千差万别,和当初本地电脑测试环境很可能不一样,有很多特殊情况没有考虑到,如果需要保 ...

  6. Docker学习之——Node.js+MongoDB+Nginx环境搭建(一)

    最近在学习Node.js相关知识,在环境搭建上耗费了不少功夫,故此把这个过程写下来同大家分享一下,今天我先来介绍一下Docker,有很多人都写过相关知识,还有一些教程,在此我只想写一下,我的学习过程中 ...

  7. Fis3构建迁移Webpack之路

    Webpack从2015年9月第一个版本横空初始至今已逾2载.它的出现,颠覆了一大批主流构建如Ant.Grunt和Gulp等等.腾讯NOW直播IVWEB团队之前一直采用Fis构建,本篇文章主要介绍从F ...

  8. nodejs--get请求数据解析

    ---- 三种方式解析: 1.自动动手切 2.api的querystring模块 3.api的url模块

  9. content-type对照表

  10. <转>记dynamic的一个小坑 -- RuntimeBinderException:“object”未包含“xxx”的定义

    →转载地址← 创建一个控制台程序和一个类库, 在控制台创建一个匿名对象,然后再在类库中访问它,代码如下: namespace ConsoleApplication1 { class Program { ...