【发布时间】:2021-03-04 09:53:03
【问题描述】:
我想制作一个组件,其 props 将直接相互依赖。
用法1(当variant="first"我必须设置height,width一定不可用):
<Example variant="first" :height="100" />
用法2(当variant="second"我必须设置width,height一定不可用):
<Example variant="second" :width="100" />
Example组件的代码:
export default defineComponent({
props: {
variant: {
type: String,
required: true,
validator: function(value) {
// The value must match one of these strings
return ['first', 'second'].indexOf(value) !== -1
}
},
height: {
type: Number,
required: true // <-- Only when variant is "first"
},
width: {
type: Number,
required: true // <-- Only when variant is "second"
}
}
}
来自 React 的示例:
const Example = (
props:
{ variant: "First"; height: number } |
{ variant: "Second"; width: number }
) => {
/*...code...*/
};
如何在 Vue 3 + TypeScript 中做到这一点?
【问题讨论】:
标签: javascript vue.js vuejs3