A
A
Alexey Alekseev2020-10-18 14:12:26
Vue.js
Alexey Alekseev, 2020-10-18 14:12:26

How to dynamically add an element with a Vue event?

If I statically add to the Vue template for example , then everything works fine, at the output we get an input with an event.

But if I add the same code dynamically, then it doesn't work, we get . those. vue doesn't recognize the bound event.

How can you dynamically add elements with vue events?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2020-10-18
@remlin1000

Vue always renders based on data.
Make an array of inputs and draw it. And on click Add - add a value to the array

{
        el: '#vu-app',
        data: {
          inputs: [],
        },
        methods : {
            addInputButton : function (event) {
               this.inputs.push('Кнопка');
            },
            myFunc(index){
              alert('Нажата кнопка '+index);
            }
        }
    }

<div id="container">
      <button v-on:click="addInputButton">Add Input</button>

      <input
        v-for="(inp, index) in inputs"
        :key="index"
        :value="inp"
        type="button"
        @click="myFunc(index)"
      />
   <div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question