Vue基础-渲染函数-父子组件-传递数据
Vue 测试版本:Vue.js v2.5.13
做了个 demo,把父子组件的数据都绑定到 Vue 实例 app 上,注释中的 template 相对好理解些
<div id="app">
<myele :level="level">
</myele>
<child :level="level">
<p>para default</p>
<span slot="span">a span</span>
<div slot="div">a div</div>
<div slot="footer" slot-scope="props">{{props.text}}</div>
</child>
</div>
window.onload = function() {
Vue.component('myele', {
render(createElement) {
return createElement('child', {
props: { level: this.level },
scopedSlots: {
footer: function(props) {
return createElement('div', [createElement('span', props.text)]);
}
}
}, [createElement('span', 'hello'), 'world']);
},
/* template:`
<child :level="level">
<span>hello</span>world
<div slot="footer" slot-scope="props">
<span>{{props.text}}</span>
</div>
</child>
`,*/
props: ['level']
});
Vue.component('child', {
render(createElement) {
let nodes0 = this.$slots.default;
let nodes1 = this.$slots.span;
let nodes2 = this.$slots.div;
let nodes3 = this.$scopedSlots.footer({
text: 'scopedSlots-foooter'
});
return createElement('h' + this.level, [nodes0, nodes1, nodes2, nodes3]);
},
props: ['level']
});
new Vue({
el: '#app',
data: {
level: 1
}
});
};
效果如下:

其实最初不是这样写的,js 是这样:
window.onload = function() {
Vue.component('myele', {
render(createElement) {
//复杂的作用域插槽单独拎出来;
let scopedSlotsNode = createElement('div', {
scopedSlots: {
footer: function(props) {
return createElement('span', props.text);
}
}
});
return createElement('child', {
props: { level: this.level }
}, [createElement('span', 'hello'), 'world', scopedSlotsNode]);
},
/* template:`
<child :level="level">
<span>hello</span>world
<div>
<span slot="footer" slot-scope="props">
{{props.text}}
</span>
</div>
</child>
`,*/
props: ['level']
});
Vue.component('child', {
render(createElement) {
let nodes0 = this.$slots.default;
let nodes1 = this.$slots.span;
let nodes2 = this.$slots.div;
let nodes3 = this.$scopedSlots.footer({
text: 'scopedSlots-foooter'
});
return createElement('h' + this.level, [nodes0, nodes1, nodes2, nodes3]);
},
props: ['level']
});
new Vue({
el: '#app',
data: {
level: 1
}
});
};
结果报错,

半天没反应过来,后来想想,明白了,解释就在 template 里,不明白的话,再看看;
参考文档:
https://cn.vuejs.org/v2/guide/render-function.html#插槽
Vue基础-渲染函数-父子组件-传递数据的更多相关文章
- Vue : props 使用细节(父组件传递数据给子组件)
props使用细节 在Vue.js中我们可以使用 props 实现父组件传递数据给子组件,下面我们总结一下props的使用细节 1.基础类型检查 2.必填数据 3.默认值 4.自定义验证函数 其中每一 ...
- vue 父子组件传递数据
单向数据流: 数据从父级组件传递给子组件,只能单向绑定. 子组件内部不能直接修改从父级传递过来的数据. 解决方法: 可以使用data将父组件传递过来的数据拷贝一份存放起来,再修改拷贝的数据就可以了 / ...
- vue2中component父子组件传递数据props的使用
子组件使用父亲传过来的数据,我们需要通过子组件的 props 选项. 组件实例的作用域是孤立的,不能在子组件的模板内直接引用父组件的数据.修改父亲传过来的props数据的时候 父亲必须传递对象,否则不 ...
- Vue基础-渲染函数-插槽
Vue 测试版本:Vue.js v2.5.13 先看个插槽的例子: <div id="app"> <child > <span slot-scope= ...
- vue $emit $on 从子组件传递数据给父组件
原理是: 子组件使用$emit发送数据,父组件使用$on,或者v-on绑定, 来监听子组件发送的数据. 子组件: <button @click="sendChildData" ...
- 前端-Vue基础3(父子组件交互)
1.子组件往父组件传值 点击子组件的值,子组件自增,父组件的值也跟着自增 通过:this.$emit('change')方法向父组件暴露事件,在子组件中引用,可以调用父组件的方法 点击子组件触发cli ...
- vue单文件组件形成父子(子父)组件之间通信(vue父组件传递数据给子组件,子组件传递数据给父组件)
看了很多文章,官网文档也有看,对父子组件通信说的不是很明白:决定自己总结一下: vue一般都使用构建工具构建项目:这样每个组件都是单文件组件:而网上很多文章都是script标签方式映入vue,组件通信 ...
- vue 父向子组件传递数据,子组件向父组件传递数据方式
父组件向子组件传递数据通过props,子组件引入到父组件中,设置一个值等于父组件的数据,通过:bind将数据传到子组件中,子组件中通过props接收父组件的数据,这样就可以使用父组件的数据了,循环组件 ...
- vue子组件使用自定义事件向父组件传递数据
使用v-on绑定自定义事件可以让子组件向父组件传递数据,用到了this.$emit(‘自定义的事件名称’,传递给父组件的数据) <!DOCTYPE html> <html lang= ...
随机推荐
- Node.js验证码模块captchapng
captchapng是一个基于pnglib模块开发,数字型验证码模块.内置字体.全JavaScript无其它依赖.不像有的验证码需要依赖canvas或者是需要编译,而且captchapng使用起来简单 ...
- connect: Network is unreachable解決方法
connect: Network is unreachable解決方法 解決方式:請確認ifcfg-eth0檔案設定 指令如下: vi /etc/sysconfig/network-scrip ...
- HTML中css和js链接中的版本号
背景 在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css.js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候, 浏览器 ...
- AngularJS实现鼠标右键事件
常规javascript鼠标右键直接在标签上加contextmenu="alert('a')"即可,现在angular通过directive来定义一个右键指令. app.direc ...
- 快速过滤出完整的SQL语句
[root@bass ca]# mysqlbinlog -- |egrep -v "^(/|SET|BEGIN|COMMITER|#|COMMIT)" >a.log [roo ...
- WIN XP 命令汇总
winver检查Windows版本dxdiag检查DirectX信息mem.exe显示内存使用情况Sndvol32音量控制程序sfc.exe系统文件检查器gpedit.msc 组策略regedit.e ...
- Ubuntu12.04 修复GRUB
电脑安装了双系统,本来好好的GRUB管理启动,在重装过之后就只能进win7了,所以尝试将GRuB重新安装到mbr,使用GRUB作为启动管理程序. 1.制作U盘系统 使用软碟通,讲Ubuntu12.04 ...
- C++的历史与现状
在31年前(1979年),一名刚获得博士学位的研究员,为了开发一个软件项目发明了一门新编程语言,该研究员名为Bjarne Stroustrup,该门语言则命名为——C with classes,四年后 ...
- 读取大csv文件数据插入到MySql或者Oracle数据库通用处理
import java.io.BufferedInputStream; import java.io.BufferedReader;import java.io.BufferedWriter;impo ...
- 绝对详细!Nginx基本配置、性能优化指南
大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...