A
A
Andrew2019-12-28 08:42:37
Vue.js
Andrew, 2019-12-28 08:42:37

How to dynamically call only those component props that are needed?

Hello. There is v-for, it generates components from an array of objects.
If there is a mask, we refer to its properties, otherwise these properties are unnecessary to us.
How to dynamically generate props for a component?

<v-col v-for="item of components_list" v-bind:key="item.id">
  <v-text-field
    v-model="components[item.code_name]"
    :label="item.title"

    // Эти свойства нужны только когда есть маска, в других случаях их не вызывать
    v-mask="item.mask"
    :hint="item.mask"
  />
</v-col>

Ps
It is possible to use if directly in the template itself to wrap props.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Suha, 2019-12-28
@andreysuha

If you don’t need any props, then pass falsely values ​​to them and already inside the component determine what is in your props and what to do with it

A
Aetae, 2019-12-28
@Aetae

The correct answer was given by Andrey Suha .
But if you really want to, then you can do something like this:

<v-col v-for="item of components_list" v-bind:key="item.id">
  <v-text-field
    v-bind="getProps(item)"   
    v-model="components[item.code_name]"
  />
</v-col>
where getPropsreturns an object of the form { [prop]: value }.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question