我们知道,在 jsx 中可以这样为 props 赋值
const props = { a: 1, b: 1,}render() { return ( <MyComponent {...props} /> )}
在 vue 中,虽然我能够这样做
<template><my-comp :some-props="props"></my-comp></template>// ...data() { return { props: { a: 1, b: 1, }, }, },
但和上述的区别在于,这样 my-comp 其实只接收了一个 some-props
的 prop, (一个对象属性),而不是像 jsx 那样,获得了 a
,b
两个 prop (值展开的属性)。
对象属性和展开属性的区别在于,前者不方便做 props 验证。
如果我想实现和 jsx 一样的效果,我就要这样写
<template><my-comp :a="props.a" :b="props.b"></my-comp></template>// ...data() { return { props: { a: 1, b: 1, }, }, },
这样写超级烦,因为经常要写好多 prop。
那么问题来了,请问在 vue 中是否可以实现 jsx 中那样的简写呢?
相关分类