函数式组件

函数式组件想像成组件里的一个函数,入参是渲染上下文(render context),返回值是渲染好的HTML。具有 Stateless(无状态)和 Instanceless(无实例)的特性。可以把它用作高阶组件。

  • 无状态:没有响应式数据
  • 无实例:没有 this 上下文

写法

写法可以直接是一个 js 对象,也可以声明组件。函数式组件没有 this,参数传递靠 context ,包括的属性有:

  • props
  • children
  • slots
  • parent
  • listeners
  • injections
  • data

其中 data 包含了其他属性的引用。

  1. Vue.component('my-component', {
  2. functional: true,
  3. render: function(creatElement, context) {
  4. // ...
  5. },
  6. // Props 可选
  7. props: {
  8. // ...
  9. }
  10. })
  1. export default {
  2. name: 'functional-component',
  3. functional: true,
  4. render(createElement, context) {
  5. return createElement('button', 'click me')
  6. }
  7. }