6.2 函数式组件
Vue提供了一种就可以让组件变为无状态、无实例的函数化组件,既然这个组件是函数,那么它的渲染开销会低很多。一搬情况下,当我们需要在多个组件中选择一个来代为渲染,或者在将children,props,data
等数据传递给子组件前进行数据处理,我们都可以用函数式组件来完成,其本质上也是对组件的一个外部包装。
6.2.1 使用场景
- 定义两个组件对象,
test1,test2
- 定义两个组件对象,
var test1 = {
props: ['msg'],
render: function (createElement, context) {
return createElement('h1', this.msg)
}
}
var test2 = {
props: ['msg'],
render: function (createElement, context) {
return createElement('h2', this.msg)
}
}
- 2.定义函数式组件
Vue.component('test3', {
// 函数式组件的标志 functional设置为true
functional: true,
props: ['msg'],
render: function (createElement, context) {
var get = function() {
return test1
}
return createElement(get(), context)
}
})
- 3.使用函数式组件
<test3 :msg="msg" id="test">
</test3>
new Vue({
el: '#app',
data: {
msg: 'test'
}
})
最终渲染的结果为:
<h2>test</h2>
6.2.2 源码分析
函数式组件会在组件的对象定义中,将functional
属性设置为true
,在根实例挂载的过程中,遇到函数式组件的占位符依然会进入创建子组件createComponent
的流程。由于functional
属性的存在,代码会进入函数式组件的分支中,并返回createFunctionalComponent
调用的结果。
function createComponent(){
···
if (isTrue(Ctor.options.functional)) {
return createFunctionalComponent(Ctor, propsData, data, context, children)
}
}
createFunctionalComponent
方法会对传入的数据进行检测和合并,实例化FunctionalRenderContext
,最终调用函数式组件自定义的render
方法执行渲染过程。
function createFunctionalComponent(
Ctor, // 函数式组件构造器
propsData, // 传入组件的props
data, // 占位符组件传入的attr属性
context, // vue实例
children// 子节点
){
// 数据检测合并
...
// 实例化
var renderContext = new FunctionalRenderContext(data,props,children,contextVm,Ctor);
// 执行render函数
var vnode = options.render.call(null, renderContext._c, renderContext)
}
而Function
这个类最终的目的是定义一个和真实组件渲染不同的render
方法。
function FunctionalRenderContext() {
// 省略其他逻辑
this._c = function (a, b, c, d) { return createElement(contextVm, a, b, c, d, needNormalization); };
}
执行render
函数的过程,又会递归调用createElement
的方法,这时的组件已经是真实的组件,开始执行正常的组件挂载流程。
从源码中可以看出,函数式组件并不会像普通组件那样有挂载组件周期钩子,监听状态和管理数据的过程,它只会原封不动的接收传递给组件的数据做处理。因此作为纯粹的函数它只做数据的处理以及渲染组件的选择,这也大大降低了函数式组件的开销。