render vue 添加类_通过render的形式来vue组件

template的形式创建的组件相对于render形式创建的组件来说局限性大一点,render形式创建的组件能很好地展现js的能力。

直接上代码

export default {

name: 'renderComponent',

props: {

componentProps: String

},

data() {

return {

componentMsg: 'componentMsg'

}

},

beforeCreate() {

console.log('beforeCreate')

},

created() {

console.log('created')

},

beforeMount() {

console.log('beforeMount')

},

mounted() {

console.log('mounted')

},

//通过render创建组件而不是通过template。没有:绑定参数的操作而是直接{}绑定参数

render(h) {

console.log('render')

return(

{this.componentMsg}

{//创建新的div节点并且绑定对应的class和style和slot插槽

h(

'div',

{

class: [

!this.componentProps ? 'font-big' : '',

this.componentProps ? 'font-weight' : ''

],

style: { color: 'green' }

},

this.$slots.a

)

}

{this.componentProps}

{

h(

'div',

{

'class': 'font-small',

style: { color: 'pink' }

},

this.$slots.b

)

}

)

}

}

>

render中同样可以定义data绑定class和style加入插槽等等功能,还有生命周期其中的顺序如下

render是介于beforemounted和mounted之前的。

绑定data数据的时候要使用this,以及双花括号绑定现在也变成了单花括号。

render最后必须返回一个用()包起来的且只有一个根div的html模板。

引用import renderComponent from './components/renderComponent.vue'

components: {

renderComponent

},

1111111111
2222222222

end!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值