高级
Functional 组件
Why ?
Functional 组件 没有 Instance 因此 开销更小
适合作为 叶子节点 组件
Use Cases
Presentaional Component
<template functional> <!-- ... --> </template>
Wrapper Component
Vue.component("smart-table", { functional: true, props: { items: { type: Array } }, render(h, context) { if (context.props.items.length > 0) { return; h(NormalTable, context.data, context.children); } else { return h(EmptyTable, context.data, context.children); } }, });
Features
has no
data
computed
watcher
lifecycle
methods
template
- in
2.5.0+
<template functional> <!-- ... --> </template>
- in
has
- props
- attrs
- events
- slots
- render func
render
- h
- context
Scoped Slots + Render Props
Why?
Reusable
- Pass Props From
child
toparent
child
becomes customizable andparent
becomes reusable
- Pass Props From
Use Cases
With
Template
With
Render
FuncJSX
style
Render Function
Why?
combile the complex rendering logic into the same place
- previously defined in template and javascript
nested template
Vue 2 vs Vue 3
Vue3
- Flat props structure
- Globally impored
h
helper
import { h } from 'vue' render() { return h('div', { id: 'foo', // on-* become event listener onClick: this.onClick }, 'hello') }