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基础-渲染函数-父子组件-传递数据的更多相关文章

  1. Vue : props 使用细节(父组件传递数据给子组件)

    props使用细节 在Vue.js中我们可以使用 props 实现父组件传递数据给子组件,下面我们总结一下props的使用细节 1.基础类型检查 2.必填数据 3.默认值 4.自定义验证函数 其中每一 ...

  2. vue 父子组件传递数据

    单向数据流: 数据从父级组件传递给子组件,只能单向绑定. 子组件内部不能直接修改从父级传递过来的数据. 解决方法: 可以使用data将父组件传递过来的数据拷贝一份存放起来,再修改拷贝的数据就可以了 / ...

  3. vue2中component父子组件传递数据props的使用

    子组件使用父亲传过来的数据,我们需要通过子组件的 props 选项. 组件实例的作用域是孤立的,不能在子组件的模板内直接引用父组件的数据.修改父亲传过来的props数据的时候 父亲必须传递对象,否则不 ...

  4. Vue基础-渲染函数-插槽

    Vue 测试版本:Vue.js v2.5.13 先看个插槽的例子: <div id="app"> <child > <span slot-scope= ...

  5. vue $emit $on 从子组件传递数据给父组件

    原理是: 子组件使用$emit发送数据,父组件使用$on,或者v-on绑定, 来监听子组件发送的数据. 子组件: <button @click="sendChildData" ...

  6. 前端-Vue基础3(父子组件交互)

    1.子组件往父组件传值 点击子组件的值,子组件自增,父组件的值也跟着自增 通过:this.$emit('change')方法向父组件暴露事件,在子组件中引用,可以调用父组件的方法 点击子组件触发cli ...

  7. vue单文件组件形成父子(子父)组件之间通信(vue父组件传递数据给子组件,子组件传递数据给父组件)

    看了很多文章,官网文档也有看,对父子组件通信说的不是很明白:决定自己总结一下: vue一般都使用构建工具构建项目:这样每个组件都是单文件组件:而网上很多文章都是script标签方式映入vue,组件通信 ...

  8. vue 父向子组件传递数据,子组件向父组件传递数据方式

    父组件向子组件传递数据通过props,子组件引入到父组件中,设置一个值等于父组件的数据,通过:bind将数据传到子组件中,子组件中通过props接收父组件的数据,这样就可以使用父组件的数据了,循环组件 ...

  9. vue子组件使用自定义事件向父组件传递数据

    使用v-on绑定自定义事件可以让子组件向父组件传递数据,用到了this.$emit(‘自定义的事件名称’,传递给父组件的数据) <!DOCTYPE html> <html lang= ...

随机推荐

  1. IT行业面试指导 计算机行业面试技巧 面试技巧

    简历篇 简历是你的的第一张脸,简历写的是否合理,是否吸引人,将决定你能否赢得宝贵的面试机会,迈出第一步! l  姓名,性别,学历,居住地,工作年限,邮箱,手机号 l  填“现居住地”,不要填成“户籍所 ...

  2. 微软收购跨平台移动应用开发商Xamarin

    微软今天宣布收购移动应用跨平台开发商 Xamarin.收购金额未知.Xamarin 提供了通过 C# 开发 iOS.Android 和 Windows 原生移动应用的工具,以及云端应用測试平台 – 全 ...

  3. shell 基础语法

    shell 基础语法 =============================================== 推荐:http://c.biancheng.net/cpp/shell/ ==== ...

  4. 使用 nice、cpulimit 和 cgroups 限制 cpu 占用率

    Linux内核是一名了不起的马戏表演者,它在进程和系统资源间小心地玩着杂耍,并保持系统的能够正常运转. 同时,内核也很公正:它将资源公平地分配给各个进程. 但是,如果你需要给一个重要进程提高优先级时, ...

  5. vue-router介绍

    vue-router学习 转自:https://my.oschina.net/u/1416844/blog/849971 1. vue-router介绍 vue-router把react-router ...

  6. homebrew 无法安装,提示不能在根目录下使用

    sudo chown -R $(whoami) /usr/local 把/use/local的owner換成自己,就有write權限了 whoami就是一個命令,會echo當前登錄用戶的名字.當然你知 ...

  7. JS - caller,callee,call,apply

    在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments : 该对象代表正在执行的函数和调用它的函数的参数. [function.]argument ...

  8. 回文串dp

    一个字符串如果从左往右读和从右往左读都一样,那么这个字符串是一个回文串.例如:"abcba","abccba". 蒜头君想通过添加字符把一个非回文字符串变成回文 ...

  9. [ADC]Linux ADC驱动

    ADC TI adc user guide: http://processors.wiki.ti.com/index.php/Linux_Core_ADC_Users_Guide 问题: 在tools ...

  10. 分布式模式之Broker模式(转)

    问题来源: 创建一个游戏系统,其将运行在互联网的环境中.客户端通过WWW服务或特定的客户端软件连接到游戏服务器,随着流量的增加,系统不断的膨胀,最终后台数据.业务逻辑被分布式的部署.然而相比中心化的系 ...