Trying to wrap my head around this example of list rendering in VueJS
I understand id starts at 0, there's a state variable for newTodo, and todos.
In the function addTodos I can see we're pushing into the todos array with and id, and a new todo value.. the bit I'm confused on there is literally that array..
I understand id: id++, it's adding 1 to the value of id, so every time you add a todo, it'll be a number higher than the previous... but where/why is text: there and not just newTodo.value++ or something? and then why am I making newTodo.value = an empty string after the push method?
6 Replies
The array in the todo ref starts with 3 example objects. Each time you add a newTodo, the addTodo function pushes a new object to the todo array. that object is the current value of id, then increments that, and stores whatever string is in the input field - that's the newTodo.value - it puts that in the newTask object with the key "text", pushes that to the todo array, and by ending with newTodo.value= '' assigns an empty string to the input field, effectively clearing it so you can add your next newTodo.
When the todo array is rendered, each object in the todo array is looped over and the text property of each is displayed
@missymae Awesome, thanks a lot 🙂 makes it much clearer to me now
I see and the "text" is also what will be in v-model inside the html itself, I remember that now. v-model effectively replaces type="text" in the input.
No. v-model allows you to synchronize an input's value (or a component prop) with a reactive value (ref or computed).
A v-model is a 2 way binding of the value to the input (change the value the input is updated, change the input the value is ipdated)
v-model
in this case is the same as this:
it's a build-in macro that can help you make your code less verbose. But sometimes you might want to handle it by your self.Gotcha. In the vue tut it uses v-model to sync the input but there’s no submit etc.. I imagine to make this a little better I’d just have to add a submit button and then clear the text value after each submit? Like in a todo list for example
There's already a submit button. Buttons are submit by default when in a form