I am starting with Vue and following the video tutorial by Traversy Media on Youtube
There he have used v-bind but I didn't proper explained what are they (or at-least I am unable to get it)
For some reason, I still find documentation slightly hard to comprehend.
So this is what we are doing.
<template>
<div id="app">
<p>{{msg}}</p>
<Todos v-bind:todos="todos" />
</div>
</template>
<script>
import Todos from "./components/todo.vue";
let todo = [
{
name: "Rohit",
title: "Full Stack Developer"
},
{
name: "Varun",
title: "head of marketing"
},
];
export default {
name: "app",
components: {
HelloWorld,
Todos
},
data() {
return {
msg: "hello",
todos: todo
};
}
};
</script>
Question:1 Can someone please comprehend this piece of code
<Todos v-bind:todos="todos" />
like what is v-bind and why we are keeping value of todos equal to string? (I understand that ultimately he is passing todos to child component as props)
And then in todo.vue, he is doing something like this
<template>
<div v-bind:key="todo.id" v-for="todo in todos">
<h3>{{todo.title}}</h3>
</div>
</template>
<script>
export default {
name: "Todos",
props: ["todos"]
};
</script>
Question:2 This is where things get pretty scary for me. To start,
In export default, why would he used an array in props? props: ["todos"]
? from the boiler plate code where they pass a string they just did something like this props: {msg: String}
so why props: ["todos"]
here?
export default {
name: "HelloWorld",
props: {
//We are defining the message type here
msg: String
}
};
Question 3: Lastly in this line right here
<div v-bind:key="todo.id" v-for="todo in todos">
<h3>{{todo.title}}</h3>
I understand the reason behind doing this v-bind:key="todo.id"
but then again what is v-bind? where do we use it?
The v-bind directive instructs Vue to keep the element's id attribute in sync with the component's dynamicId property. If the bound value is null or undefined , then the attribute will be removed from the rendered element.
v-bind directive allow you to produce some dynamic value by typing some JS expression that in most cases depends on the data from data model - so think about v-bind as directive that you should use when you want deal with some dynamic things. In some case you can use each of them.
Binding Classes Dynamically To help with this, the v-bind:class directive provides a way to bind classes to content in an element. For example, you may need to underline the element's text and change the color and font-weight.
It is used for binding data, attributes, expressions, class, styles.
Q1
In Vue you can pass props to child components. If you would have used: todos="todos"
. todos
prop would be equal to the string todos
however with v-bind:todos
or in short :todos
it makes the prop the javascript variable todos
. Which is the todos
from the data
function
Q2
In vue you can reference the props in different ways. Declaring them in an array is just shorter but has a drawback which is that you can't validate the props. When declaring the props in an object you can specify the type of the prop eg. String, Array, Object etc. It's also possible to declare a default and specifiy required props.
props: {
msg: {
type: String,
required: true,
default: ''
}
}
Q3
The example provided won't work as there is no id
in the let todo
just name
and title
. So what would work is:
<div v-bind:key="i" v-for="(todo, i) in todos">
Again it's possible to just use :key="i"
. The key could be seen as an id
. It's used so Vue knows which element is which in a loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With