javascript - How vue template simulates the destructuring props feature in jsx
PHP中文网
PHP中文网 2017-05-19 10:19:07
0
2
1006

We know that in jsx you can assign values ​​to props like this

const props = {
  a: 1,
  b: 1,
}

render() {
  return (
    <MyComponent {...props} />
  )
}

In vue, although I am able to do this

<template>
<my-comp :some-props="props"></my-comp>
</template>

// ...
data() {
  return {
    props: {
      a: 1,
      b: 1,
    },
  },
},

But the difference from the above is that my-comp actually only receives a prop of some-props (an object attribute), instead of getting a## like jsx #, b Two props (properties with value expansion).

The difference between object properties and expanded properties is that the former is inconvenient for props verification.

If I want to achieve the same effect as jsx, I have to write like this

<template>
<my-comp :a="props.a" :b="props.b"></my-comp>
</template>

// ...
data() {
  return {
    props: {
      a: 1,
      b: 1,
    },
  },
},

It’s super annoying to write like this, because you often have to write a lot of props.

Then the question is, is it possible to implement the abbreviation in jsx in vue?

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
我想大声告诉你

Pay attention. Also, what about this?

<template>
<my-comp :vprops="props"></my-comp> //在组件里直接用vprops.a,vprops.b
</template>

// ...
data() {
  return {
    props: {
      a: 1,
      b: 1,
    },
  },
},
習慣沉默

Then write jsx in the render function instead of template? Escape

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!