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 ...
随机推荐
- mysql语法 -- concat函数
mysql CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL.或许有一个或多个参 ...
- 基于Java的数据采集(二)
在上一篇文章<基于Java的数据采集(一)>:http://www.cnblogs.com/lichenwei/p/3904715.html 提到了如何如何读取网页源代码,并通过group ...
- android 软键盘自动弹出和关闭
一.布局中软键盘自动弹出 bargain_dialog_offer_edit.requestFocus(); bargain_dialog_offer_edit.setFocusable(true) ...
- bootstrap入门基础
1.字体 text-left text-center text-right text-lowercase 小写 text-uppercase 大写 text-capitalize 首字母大写 2.表格 ...
- 【Dubbo 源码解析】01_Dubbo 设计简介
Dubbo 设计简介 Dubbo 采用 Microkernel + Plugin (微内核 + 插件)模式,Microkernel 只负责组装 Plugin,Dubbo 自身的功能也是通过扩展点实现的 ...
- memcpy in place 数组内拷贝
首先看一段代码 #include <stdio.h> #include <pthread.h> int main(){ ]; ; ; i++) { t1[i] = i; pri ...
- k8s(1)-使用kubeadm安装Kubernetes
安装前准备 1. 一台或多台主机,这里准备三台机器 角色 IP Hostname 配置(最低) 操作系统版本 主节点 192.168.0.10 master 2核2G CentOS7.6.1810 工 ...
- 跟bWAPP学WEB安全(PHP代码)--XPath注入
XML/Xpath注入 看了下,A2里面是认证与会话管理的破坏或称之为绕过,没有特别要写的,很多就是小问题,可能会将这类问题放在最后写一下.一篇博客,这里还是更多的着重在能够获取信息或者服务器权限的漏 ...
- MacOS install configure php-fpm
php-fpm 是预装在mac os上的,你只需要配置就好了.这个服务监听9000端口. 1. 为配置文件准备一些目录 mkdir -p /usr/share/php/var/run mkdir -p ...
- Windows下MySQL的绿化与精简
MySQL本身就支持安装使用,本文只是对自己使用免安装版MySQL的经历记录下来,以便以后查看. 首先是获取Windows下的MySQL免安装版本,这个需要去到MySQL官网进行下载.我一般喜欢把首页 ...