vue functional函数式组件


第1种写法

test.vue

<template functional>
  <div class="test">
    {{props.test.name}}
  </div>
</template>

<script>
export default {
  props: {
    test: Object,
    default: () => ({})
  }
}
</script>

父组件 index.vue

<template>
  <div>
    <test :test="{name: 'vue'}"></test>
  </div>
</template>
<script>
import test from './test'
export default {
  components: {
    test
  }
</script>

第2种写法

test.js

import Vue from 'vue'

Vue.component('test', {
  functional: true,
  props: {
    test: {
      type: Object,
      required: true
    }
  },
  render: function (createElement, context) {
    return createElement(
      'div',
      {
        attrs: {
          class: 'test'
        }
      },
      [context.props.test.name]
    )
  }
})

父组件 index.vue

<template>
  <div>
    <test :test="{name: 'vue'}"></test>
  </div>
</template>
<script>
import './test.js'
</script>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM