vue Render函数的使用方法

2020-09-24  本文已影响0人  贞贞姐
image.png

原来代码

<script>
  export default {
    name: 'Item',
    functional: true,
    props: {
      icon: {
        type: String,
        default: ''
      },
      title: {
        type: String,
        default: ''
      }
    },
    render(h, context) {
      const { icon, title } = context.props
      const vnodes = []
 
      if (icon) {
        vnodes.push(<svg-icon icon-class={icon}/>)
      }
 
      if (title) {
vnodes.push(<span slot='title'class='system-menu-title'>{(title)}</span>)
      }
      return vnodes
    }
  }
</script>

改进之后

const { icon, title } = context.props
    const vnodes = []
    if (icon) {
      const elHtml  = createElement('svg-icon',{
        attrs: {
          'icon-class': icon
        }
      })
      vnodes.push(elHtml)
    }
    if (title) {
      const elHtml  = createElement('span',{
        attrs: {
          slot: title,
        }
      },title)
      vnodes.push(elHtml)
    }
    return vnodes
上一篇 下一篇

猜你喜欢

热点阅读