Open Mario34 opened 4 years ago
尤大在关于vue/jsx的回复 vuejs/jsx/issues/141#issuecomment-658304273,尽可能少的添加语法糖到jsx中,因为选择使用jsx的用户未必一定使用模版语法
I've seen some great discussions here, while I don't have definitive answers to all the questions yet, I'll just write down some of my thoughts here:
- I don't think we should add too much syntax sugar into JSX. If we do, we should do it in a type-safe and JSX-idiomatic fashion (i.e camelCase). For example I like the idea of vModel={[val, [modifiers]]}.
在官方提供最新版本支持前、可以使用上面的两个社区的解决方案
jsx刚开始在react中对类型的支持也不好,后来react和ts团队合作再有了现在的完美支持。
Edit by @yyx990803 : there are currently two JSX transform implementations for Vue 3 with slightly differing syntax (for Vue specific features). We are using this thread to unify the design and land on an official specification of how Vue features should be handled in JSX.
希望能够得到jsx官方的支持
Vue对具有type定义的道具进行运行时验证。为了将这些类型提供给TypeScript,我们需要使用以下方法强制转换构造函数PropType:
import { defineComponent, PropType } from 'vue'
interface ComplexMessage {
title: string
okMessage: string
cancelMessage: string
}
const Component = defineComponent({
props: {
name: String,
success: { type: String },
callback: {
type: Function as PropType<() => void>
},
message: {
type: Object as PropType<ComplexMessage>,
required: true,
validator(message: ComplexMessage) {
return !!message.title
}
}
}
})
https://v3.vuejs.org/guide/typescript-support.html#annotating-return-types
目前vue.3.x还处于beta状态,官方的英文文档v3.vuejs.org
使用tsx模式开发组件
尽管模版的写法一直是官方推荐的,除了没法支持类型提示,也没有可挑剔的了。这里选择TypeScript,所以采用tsx的模式代替template
为了实现tsx的加载,需要借助相关的loader插件 @ant-design-vue/babel-plugin-jsx、HcySunYang/vue-next-jsx