I am trying to increment an array value when I add a new row in Vue.js, but I get the error:
You may have an infinite update loop in a component render function.
The JavaScript:
new Vue({
el: '#app',
data: () => ({
inputs: [{
id: 0,
username: ''
}],
}),
methods: {
add() {
this.inputs.push({ id: this.id++, username: '' });
}
}
});
The markup:
<div id="app">
<div v-for="input in inputs">
<input :key="input.id" :name="'username_' + input.id" type="text" v- v-model="input.username" placeholder="username">
</div>
<button @click="add">Add</button>
</div>
However, if I hardcode the value in the add function it works the first time:
add() {
this.inputs.push({ id: 1, username: '' });
}
So, how can I make it dynamic? Typing id: this.id++ does not work.