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. Atitit. 悬浮窗口的实现 java swing c# .net c++ js html 的实现

    Atitit. 悬浮窗口的实现 java swing c# .net c++ js html 的实现 1. 建立悬浮窗口引用代码 1 1.1. 定义悬浮窗口,设置this主窗口引用,是为了在悬浮窗口中 ...

  2. JMeter学习笔记(四)

    1. 断言 断言组件是通过获取服务器响应数据,然后根据断言规则去匹配这些响应数据:匹配到是正常现象,此时我们看不到任何提醒,如果匹配不到,即出现了异常情况,此时JMeter就会断定这个事务失败,那么我 ...

  3. java学习之实例变量初始化

    实例变量的初始化方法 第一种:通过构造函数进行初始化. 第二种:通过声明实例字段初始化. 第三种:通过对象代码块初始化. 通过构造函数进行初始化方法 通过构造函数进行对象初始化,必须在类中声明一个带参 ...

  4. java:常用的两种设计模式(单例模式和工厂模式)

    一.单例模式:即一个类由始至终只有一个实例.有两种实现方式(1)定义一个类,它的构造方法是私有的,有一个私有的静态的该类的变量在初始化的时候就实例化,通过一个公有的静态的方法获取该对象.Java代码  ...

  5. nyoj16矩形嵌套(第一道dp关于dag的题目)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题意:有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...

  6. Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解

    本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...

  7. ptxdist for sama5d3

    http://www.vahanus.net/~csc/scm/ptxdist-at91sama5d3-xpld.git/

  8. OS X删除自带的safari和facetime等程序

    打开终端 cd /Applications/ //在应用程序文件目录删除苹果自带的程序 sudo rm -rf Safari.app/ //删除safari浏览器 sudo rm -rf Mail.a ...

  9. 监听过多,会抛tooManyListener例外

    在生成一个窗体的时候,点击窗体的右上角关闭按钮激发窗体事件的方法:窗体Frame为事件源,WindowsListener接口调用Windowsclosing(). 为了配合后面的实现,我们必须将Win ...

  10. 【BZOJ】1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛(dp/-bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1616 我觉得bfs是可过的,但是交bfs上去是wa? 然后没办法看dp,原来这bfs能和dp联系在一 ...