-
Notifications
You must be signed in to change notification settings - Fork 12
TSX 简明语法
Kagol edited this page Feb 27, 2021
·
1 revision
参考:
使用大括号包裹,支持任何有效的 JavaScript 表达式,比如:2 + 2
,user.firstName
,formatName(user)
。
const name = 'Vue DevUI'
const element = <h1>Hello, { name }</h1>
tsx本身也是一个条件表达式。
const element = (name) => {
if (name) {
return <h1>Hello, { name }</h1>
} else {
return <h1>Hello, Stranger</h1>
}
}
const element = icon ? <span class="icon"></span> : null;
const data = [{
id: 1,
title: '通用'
}, {
id: 2,
title: '导航'
}]
const element = data.map(item => {
return <div>{ item.title }</div>
})
const href = 'https://devui.design'
const element = <a href={href}></a>
const element = <div className={`devui-accordion-item-title ${ disabled ? 'disabled' : '' }`}></div>
const width = '100px'
const element = <button style={{ width: width, fontSize: '16px' }}></button>
注意:
- class -> className
- tabindex -> tabIndex
button.tsx:
import { defineComponent } from 'vue'
import './button.scss';
type IButtonType = 'button' | 'submit' | 'reset';
export default defineComponent({
name: 'd-button', // 组件名
props: { // 输入/输出
type: {
type: String as () => IButtonType,
default: 'button'
},
btnClick: {
type: Function as unknown as () => ((event: MouseEvent) => void)
}
},
setup(props, ctx) { // 模板和逻辑
const { type, btnClick } = props;
return () => {
return <button
type={type}
onClick={btnClick}
>{ ctx.slots.default?.() }</button>
}
}
})