1_02 Vue Slot
slot 插槽
- 插槽内容
const component ={
template: `
<div>
<slot></slot>
</div>
`
}
new Vue ({
components: {
com: components
},
el: '#app',
template: `
<div>
<com>
this is contnent.
</com>
</div>
`
})
- 具名插槽
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>
`
});
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的更多相关文章
- vue自学入门-4(vue slot)
vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...
- vue slot nested bug
vue slot nested bug slot name bug Error <slot name="global-system-guide-slot"></s ...
- Vue slot 插槽用法:自定义列表组件
Vue 框架的插槽(slot)功能相对于常用的 v-for, v-if 等指令使用频率少得多,但在实现可复用的自定义组件时十分有用.例如,如果经常使用前端组件库的话,就会经常看到类似的用法: < ...
- Vue slot插槽
插槽用于内容分发,存在于子组件之中. 插槽作用域 父级组件作用域为父级,子级组件作用域为子级,在哪定义的作用域就在哪. 子组件之间的内容是在父级作用域的,无法直接访问子组件里面的数据. 插槽元素 &l ...
- Vue slot插槽内容分发
slot插槽使用 使用场景,一般父组件中又一大段模板内容需要运用到子组件上.或者更加复杂的,子组件需要运用到父组件大段模板内容,而子组件却不知道挂载的内容是什么.挂载点的内容是由父组件来决定的. Sl ...
- vue slot 复用
话不投机半句多,直接上代码 有3步 第一步:创建渲染slot的组件 重要 第二步:为slot添加父组件数据(props) 重要 第三步:使用 第一步:创建渲染slot的组件 首选创建一个单文件组价,由 ...
- vue slot
一直觉得vue的slot比较申请,而且比较深奥,总有点不想用的感觉,事实上,在一定程度上,也真的可以完全避开slot就能把一个项目完全搭建完成. 但是随着用的次数越来越多,看到的内容也越来越多的情况, ...
- vue slot插槽的使用方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue slot简单理解
情形一: 子组件定义了具名的slot,父组件使用具名的slot,slot显示顺序为子组件定义slot的顺序 子组件: Vue.component('child',{ template:`<div ...
随机推荐
- Windows 使用 Gogs 搭建 Git 服务器
随便说两句 之前有使用 Gitblit 在Windows搭建Git服务器,用的也挺好的,可能安装起来略麻烦一点.现在全用 Gogs 在windows搭建Git服务器,主要是因界面好看,管理更方便一些. ...
- Java知多少(62)线程同步
当两个或两个以上的线程需要共享资源,它们需要某种方法来确定资源在某一刻仅被一个线程占用.达到此目的的过程叫做同步(synchronization).像你所看到的,Java为此提供了独特的,语言水平上的 ...
- 【转】JS获取浏览器可视区域的尺寸
from: http://www.xiaoboy.com/detail/1341545044.html 所谓可视区域是指能看得见的区域,即在浏览器中能看到页面的区域(高度与宽度).刚刚使用 docum ...
- 转载——githup的提交
Github是管理软件开发的首选托管网站,12306的火车票插件一时让国内当时很多小白开发者(当然也包括我)认识到了这个网站.GitHub可以托管各种git库,并提供一个web界面,与 SourceF ...
- Secure backup
Secure backup 安全备份软件 安全备份软件致力于提供一款开源免费的安全云备份软件,支持文件管理,文件自动同步到云盘,增量备份等功能. 目前正在开发过程中...
- Javascript Base64加密解密代码
<script language="javascript" runat="server"> var keyStr = "ABCDEFGHI ...
- 基金 、 社保和QFII等机构的重仓股排名评测
来源:基金前20大重仓股持仓股排名 基金前15大重仓股持仓股排名 基金重仓前15大个股,相较于同期沪深300的平均收益, 近1月:2.23%, 近3月:3.09%, 近6月:9.61%, 近1年:13 ...
- duilib进阶教程 -- TreeView控件的bug (9)
一.不自动调整子控件的宽度(TreeView宽度小于260时) 相信亲们用同样的代码,显示效果肯定和Alberl不一样吧~O(∩_∩)O~ 嘿嘿,仔细对比下,看看你们的列表头背景图片是下面这样的么? ...
- Spring学习笔记--Spring简介
1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...
- 使用Eclipse的坑
1.运行Eclipse时突然出现找不到或者无法加载主类,这个问题不解决,下面的学习就无从做起,查了网上的一些资料,无法解决,所以还是有点烦人.如果在解决问题的过程中能够学到点什么,也是很值得的,但是就 ...