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= ...
随机推荐
- 雅虎天气API调用
雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...
- [svc]ip routing和no ip routing
ip routing: 查路由表, 如果ping的目的在RT中没有,不发出任何包(arp也不会发出) 如果RT中存在,则arp 下一跳,相当于no ip routing+配置网关 注: 静态路由: 指 ...
- Path相关方法解说(二)
今天咱们一起来看看Path里 XXXTo 相关的一类方法. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/4 ...
- Cocos2d-x 3.x 如何编译成安卓程序
1.安装JDK 2.安装eclipse,安卓官方现在不提供eclipse for android,只好自己配置了.首先安装一个eclipse,在Help——Install New SoftWare中安 ...
- python-爬图小样
python-爬某页面图 注意:python3+版本与python2有一定区别,需要注意多点. #! /usr/bin/env python3.5.4 # coding=utf-8 # 爬百度某贴吧页 ...
- 改变placeholder的样式
input::-webkit-input-placeholder{ font-size:12px; color:#d2d2d2 }
- input 和<fmt:formatDate>的结合使用
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <inpu ...
- C - The C Answer (2nd Edition) - Exercise 1-2
/* Experiment to find out what happens when printf's argument string contains \c, where c is some ch ...
- buildroot 修改root密码后无法登录ssh解决方法
客户说想修改root密码后再登录ssh, 研究了一下,是因为ssh登录是匹配了之前的 密码生成文件,只要把之前的密码生成文件删除就可以. 过程如下: 删除 /etc/ssh/ssh_host*. rm ...
- 常见linux内核线程说明
ps进程名有方括号的是内核级的进程,执行辅助功能(比如将缓存写入到磁盘):所有其他进程都是使用者进程.您会注意到,就算是在您新安装的(最小化的)系统中,也会有很多进程在运行. 在文档kernel-pe ...