I would like to push the input to the array. When I press the button to add the items nothing happens. I've searched multiple websites but it seems that I am too dumb to find the right code :( Do anyone know whats the problem with the code:
<!DOCTYPE html>
<head>
<title>Clientseitige Webentwicklung mit Vue.js</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app_basic">
<ul>
<li v-for="value in cars">{{ value }}</li>
</ul>
<input id="input1" type="text" v-model="newFarbe" placeholder="farbe">
<input id="input2" type="text" v-model="newMarke" placeholder="marke">
<input id="input3" type="text" v-model="newPS" placeholder="ps">
<button v-on:click="addCar">Hinzufügen</button>
</div>
<script>
var app_basic= new Vue(
{
el: '#app_basic',
data: {
cars:{
farbe: 'Blau',
marke: 'Ford',
ps: '240'
}
},
methods:{
addCar: function() {
this.cars.push({farbe : this.newFarbe, marke : this.newMarke, ps : this.newPS});
this.newFarbe='';
this.newMarke='';
this.newPS='';
}
}
});
</script>
</body>
</html>
carsis not an array so it doesn't have a.pushmethod. b)newFarbe,newMarkeandnewPsare not defined indata, therefore not reactive, therefore not linked to your model (in other words, they are alwaysundefinedin your component's model - vue is not informed about their changes). The answer provided fixes both, although it doesn't call each one out individually.new. Here's what I'm talking about: jsfiddle.net/websiter/v2wrmbc8. You also don't needids on inputs (you don't have labels to target them, so...)