创建一个自定义组件

<template>
  <ul class="h-tag-close-tip">
    <li v-for="item in contextMenuData.menulists" :key="item.optionName" class="h-tag-close-tip-item" @click.stop.prevent="fnHandler(item)">
      <h-icon :name="item.iconName"></h-icon>
      <span>{{item.optionName}}</span>
    </li>
  </ul>
</template>
<script>
export default {
  name: 'ContextMenu',
  data () {},
  props: {
    contextMenuData: {
      [...]
    }
  },
  methods: {
    fnHandler (item) {
      this.$emit(item.fnHandler)
    }
  }
}
</script>

<style scoped>

</style>

在父组件使用render h函数渲染

vue渲染自定义组件的坑-20190326

import ContextMenu from '../../../components/ContextMenu'
export default {
  name: 'sqlDev',
  components: {
    ContextMenu,
    ....
  }

h(‘ContextMenu’, {
          props: {
            contextMenuData: self.contextMenuData
          },
          style: {
            width: '80px',
            height: '60px'
          },
          on: {
            savedata () {
              alert(self.selectScript.entity.uuid)
            },
            newdata () {
              alert('new')
            }
          }
        })

页面报错

Unknown custom element: <ContextMenu> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

vue渲染自定义组件的坑-20190326

解决

去掉h函数参数中的引号(对比上图)
vue渲染自定义组件的坑-20190326

总结

h函数渲染一般的html元素,如div span等,需要加引号,但渲染自定义组件时,直接是应用组件引用即可

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案