Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  103] [ 4]  / answers: 1 / hits: 15910  / 9 Years ago, wed, january 13, 2016, 12:00:00

I'm trying out vuejs by following along with the laracasts series of webcasts on this. In https://laracasts.com/series/learning-vue-step-by-step/episodes/8, Jeffery Way discusses custom components. I have the following code based on his screencast:



    <div id=app>
<tasks list=tasks></tasks>
</div>

<template id=tasks-template>
<ul>
<li :class={'completed' : task.c} @click = task.c = ! task.c v-for =task in tasks>{{task.t}}</li>

</ul>

</template>

<script src=https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.12/vue.js></script>
<script type=text/javascript>

vue.component('tasks', {
template: '#tasks-template',

props:['list'] // why not props:['tasks'] ??

});

new Vue({
el: '#app',
data: {
tasks: [
{t: 'go to doctor', c: false},
{t: 'go to work', c: false},
{t: 'go to store', c: true}
]

}


In this he discusses setting the props as follows:



    props:['list'] 


Why is it not



props:['tasks'] ?


In http://vuejs.org/guide/components.html#Props it states:




Every component instance has its own isolated scope. This means you cannot (and should not) directly reference parent data in a child component’s template. Data can be passed down to child components using props.A “prop” is a field on a component’s data that is expected to be passed down from its parent component. A child component needs to explicitly declare the props it expects to receive using the props option:




How does the component know to associate the tasks array with list? Also in this case I assume child = component and parent = the vue instance?


More From » vue.js

 Answers
56

The property on your component is called list and the value passed to it is tasks.



Let's see this. First you have your main Vue instance attached (mounted) to the element with identifier #app. So this is your starting point.



<div id=app>
<tasks list=tasks></tasks>
</div>


inside of your div you have a <tasks> tag. That tag correspond to a child component, so




child = component and parent = the vue instance




Is correct. The <tasks> component is an extension of the Vue class, which has only a property declared like list. The important thing here are scopes. Notice that the list property belongs to the tasks components, and has no value in its declaration, and the value passed to it on the template (everything inside the #app div) belongs to the parent Vue instance (declared on the data of the Vue instance). So why don't props:['tasks']? Because the <tasks> component has no tasks data or property. if you actually declared the property like tasks, you would have to write your template as follows



<div id=app>
<tasks tasks=tasks></tasks>
</div>


that would be a litle confusing. So thats why the prop is list and because of the declaration list=tasks is that the component know that the list property has the value of the parent tasks array.


[#63746] Monday, January 11, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacklynr

Total Points: 542
Total Questions: 120
Total Answers: 95

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
jacklynr questions
Fri, Jan 15, 21, 00:00, 3 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Thu, Jun 4, 20, 00:00, 4 Years ago
;