tsx在vue3中的使用

1. 函数式组件

export default () => <div>test</div>

2. defineComponent

export default defineComponent({
    render() {
        return <div>tset</div>
    }
})

3. defineComponent + setup

摒弃 this ,对于ts支持最好,借助 babel-plugin-jsx

import { defineComponent, withModifiers, ref } from 'vue'

export default defineComponent({
    directives: {
        focus: {
            mounted(el) {
                el.focus()
            }
        }
    },
    emits: ['click']
    setup (props, { slots, emit }) {
        const count = ref(0)

        const inc = () => {
            count.value++
            emit('click')
        }

        const list = ref<string[]>(['a', 'b', 'c'])

        return () => { // 这个函数理解为渲染函数,在其中返回最终想要返回的东西
            v-if
            const span = true ? <span>A</span> : <span>B</span>
            
            return <div onClick => {withModifiers(inc, ['self'])}>
                test: {count.value}
                <input type="text" v-focus v-model={count.value} />
                <div>{ span } </div> 
                <ul>
                    {list.value.map((str) => <li key={str}>{str}</li>)}
                </ul>
                {/* 默认插槽内容 */}
                <div>{ slots.default ? slots.default() : 'default content' }</div>
                <div>{ slots.title ? slots.title() : 'title slot content' }</div>
                {/* <Test v-slot={{
                    title: () => <h3>title</h3>
                }}>abc</Test> */}
            </div>
        }
    }
})

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值