<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Friday Godswill</title>
    <description>The latest articles on DEV Community by Friday Godswill (@faradayyg).</description>
    <link>https://dev.to/faradayyg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F29713%2Fff5e73f6-99f7-48c8-8f19-1eddcbfda8de.jpg</url>
      <title>DEV Community: Friday Godswill</title>
      <link>https://dev.to/faradayyg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/faradayyg"/>
    <language>en</language>
    <item>
      <title>Components in VueJs</title>
      <dc:creator>Friday Godswill</dc:creator>
      <pubDate>Wed, 06 Jun 2018 18:59:14 +0000</pubDate>
      <link>https://dev.to/faradayyg/components-in-vuejs-j5g</link>
      <guid>https://dev.to/faradayyg/components-in-vuejs-j5g</guid>
      <description>&lt;p&gt;Components make our codes reusable and enable us to stick to the software development principle of D.R.Y (Don’t Repeat Yourself).  Components are blocks of code which extend basic HTML markup behaviour and can be used over and over for different purposes. What this means is that components in Vue look like basic HTML elements, but they are more configurable and hence, can perform more functions than a plain HTML element. Components can also contain other components, this makes our front end very robust and quite modular.&lt;/p&gt;

&lt;p&gt;Modern web applications are composed of many parts, and the best way to keep track of all the moving parts would be to abstract them into different small parts (components), making them easy to structure, use and maintain. So that in the end, you might end  up with code similar to this for an entire page, that performs a lot of functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'app'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;app-header&lt;/span&gt; &lt;span class="na"&gt;:links=&lt;/span&gt;&lt;span class="s"&gt;"links"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-header&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;app-sidebar&lt;/span&gt; &lt;span class="na"&gt;:items=&lt;/span&gt;&lt;span class="s"&gt;"items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-sidebar&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;app-body&amp;gt;&amp;lt;/app-body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;app-footer&amp;gt;&amp;lt;/app-footer&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;    

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’d agree with me that as a maintainer, code like this is very neat and straight to the point, and it would not take too much time to figure out what’s going on and what section does what.&lt;/p&gt;

&lt;p&gt;Components in Vue can be created in two ways, they can be created in a separate file, then imported using the Es6  &lt;code&gt;import&lt;/code&gt; statement. Or they can be registered inside the base JavaScript file and used directly.&lt;/p&gt;

&lt;p&gt;For the purpose of this article, we are going to create a basic component that takes in a user object, outputs a list, and alerts the user details when each user is clicked. &lt;br&gt;
With this, we will demonstrate:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating Components,&lt;/li&gt;
&lt;li&gt;Passing Data to components via Props,&lt;/li&gt;
&lt;li&gt;List Rendering,&lt;/li&gt;
&lt;li&gt;Emitting events from a child component,&lt;/li&gt;
&lt;li&gt;Listening for events on a parent component, and&lt;/li&gt;
&lt;li&gt;Handling Emitted events&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you are new to Vue, you can check out the &lt;a href="https://vuejs.org/v2/guide/" rel="noopener noreferrer"&gt;Official Documentation&lt;/a&gt; to get started.&lt;/p&gt;

&lt;p&gt;If you'd like to skip on ahead to the finished project, there's a &lt;a href="https://codepen.io/faradayyg/pen/gKwpqQ" rel="noopener noreferrer"&gt;Code pen&lt;/a&gt; where the finished project is hosted.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting Up
&lt;/h2&gt;

&lt;p&gt;There are two ways to setup your Vue project. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the Webpack build tool or&lt;/li&gt;
&lt;li&gt;Using Vue via the Vue CDN. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let us explore defining our components inside our JavaScript file, using the Vue CDN.&lt;/p&gt;

&lt;p&gt;To get started, make a plain old HTML File, and include the Vue JavaScript file from the CDN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes sure that Vue is available in the project. Next, we add a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag inside the body tag. This is where our Code will live.&lt;/p&gt;

&lt;p&gt;By now, Your Project should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;   

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="c1"&gt;//Our Codes, Here            &lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are now ready to begin writing our code. &lt;br&gt;
Just to make sure that Vue Js is properly installed, let’s create a “Hello Vue” application.&lt;br&gt;
Add a div tag within the body tag of your HTML file, give it an id of “app". &lt;br&gt;
Within the &lt;code&gt;div&lt;/code&gt; tag, copy and paste this:  &lt;code&gt;{{message}}&lt;/code&gt; the text in the double curly braces indicate a variable in Vue.&lt;/p&gt;

&lt;p&gt;Within your script tag, copy and paste the following, we will explore what these mean in a moment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we have done above is, &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Instantiate a new Vue Object, telling it that the HTML element it should use, has an ID of &lt;code&gt;app&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We have then provided a data object, that returns &lt;code&gt;message&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finally, we printed out the message variable defined in the data object above, into the HTML by typing in this: &lt;code&gt;{{message}}&lt;/code&gt; the double curly braces indicate that their content is a variable. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By now, our entire Code Should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            {{message}}
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
       &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
           &lt;span class="na"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
              &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                 &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run this in the browser, we should get an output of “Hello Vue”.&lt;br&gt;
Now that we have confirmed that Vue is properly setup, let’s get right to defining components in Vue.&lt;/p&gt;
&lt;h2&gt;
  
  
  Defining Our Component.
&lt;/h2&gt;

&lt;p&gt;As previously stated, components can be created as a separate file altogether, or directly inside our main JavaScript file. For this tutorial, we are going to be defining our components directly inside our JavaScript file.&lt;/p&gt;

&lt;p&gt;Components are registered using  &lt;code&gt;Vue.component(&lt;/code&gt;&lt;code&gt;'&lt;/code&gt;&lt;code&gt;tag-name&lt;/code&gt;&lt;code&gt;'&lt;/code&gt;&lt;code&gt;, options)&lt;/code&gt; command, where &lt;code&gt;tag-name&lt;/code&gt;  is the name that you want your component to bear and &lt;code&gt;options&lt;/code&gt; is an object that defines the behaviour of the component. This makes the component globally available within the file and thus, can now be used in different cases.&lt;/p&gt;

&lt;p&gt;Let’s start by defining a dummy component that does nothing but show a message on the screen. Let’s call this &lt;code&gt;user-list&lt;/code&gt;. To follow along, create a new HTML file, or modify the "hello vue" file to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;user-list&amp;gt;&amp;lt;/user-list&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user-list&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;template&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;div&amp;gt;I am a component&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

      &lt;span class="p"&gt;});&lt;/span&gt;

       &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
           &lt;span class="na"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

       &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we have done is create a Vue component, named it “user-list” and then within our HTML, we have used &lt;code&gt;user-list&lt;/code&gt; like a normal HTML tag. This is how you output your component to the front end.&lt;br&gt;
 You can see the &lt;code&gt;template&lt;/code&gt; attribute in the Vue component definition, this specifies the HTML tags that will be output by the component when mounted. Take note, that a Vue component can only have one root element.&lt;br&gt;
That’s all there is to creating a basic component in Vue. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310225%2F1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310225%2F1.png" alt="Our Component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although this works, it is not very useful as it does not demonstrate the power and reusability of components. Let’s extend this component further by defining props. &lt;/p&gt;
&lt;h2&gt;
  
  
  Props and Components
&lt;/h2&gt;

&lt;p&gt;Each Vue component lives in a scope of its own and should not access data from the outside.&lt;br&gt;
Props give us a means of passing data from a parent component (outside), to a child component. In our case, we will be passing data from &lt;code&gt;app&lt;/code&gt; to the  &lt;code&gt;userList&lt;/code&gt; component. But before we can do this, we have to explicitly specify the props we are expecting in our &lt;code&gt;user-list&lt;/code&gt;  component. Add another attribute to the &lt;code&gt;user-list&lt;/code&gt; component, call it &lt;code&gt;props&lt;/code&gt; this will be an array of all the props we are expecting to be passed to the &lt;code&gt;user-list&lt;/code&gt; component. Let the content of the props attribute be &lt;code&gt;['users']&lt;/code&gt; While we are at it, let’s modify the &lt;code&gt;template&lt;/code&gt; attribute, and remove all contents of the &lt;code&gt;div&lt;/code&gt;  replacing them with &lt;code&gt;{{users}}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also in the main HTML file, let’s add a new attribute called “users” to the &lt;code&gt;&amp;lt;user-list&amp;gt;&lt;/code&gt; tag, and set the value to &lt;code&gt;users=&lt;/code&gt;&lt;code&gt;"&lt;/code&gt;&lt;code&gt;list of users&lt;/code&gt;&lt;code&gt;"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Right now, our app’s source code should look somewhat like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;user-list&lt;/span&gt; &lt;span class="na"&gt;users=&lt;/span&gt;&lt;span class="s"&gt;"list of users"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/user-list&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userList&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;template&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;div&amp;gt;{{users}}&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

      &lt;span class="p"&gt;});&lt;/span&gt;

       &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
           &lt;span class="na"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

       &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything works correctly, the output should be this: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310222%2F2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310222%2F2.png" alt="Displaying data passed as Prop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, our component has become a little smarter, data can now be passed from the parent to it, using &lt;code&gt;users&lt;/code&gt; attribute. &lt;/p&gt;

&lt;p&gt;This does not mean that only strings can be passed as props, variables can be passed as well, using the &lt;code&gt;v-bind&lt;/code&gt; Vue attribute. Let’s extend things a little further. In our main Vue app, we will define a data attribute and pass in the variable which will be used by the Vue component. The data attribute will now be this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
         &lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="na"&gt;allUsers&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="na"&gt;about&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Really nice guy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane Dean&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loves eggs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Clark Kent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not your everyday reporter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
              &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This basically just returned an array of three objects with two keys each,  &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;about&lt;/code&gt;. &lt;br&gt;
To pass our newly defined list of users to the component, we simply add the &lt;code&gt;v-bind:users&lt;/code&gt; attribute to the component, and pass the name of the array to it, hence we have &lt;code&gt;&amp;lt;user-list v-bind:users=&lt;/code&gt;&lt;code&gt;"&lt;/code&gt;&lt;code&gt;allUsers&lt;/code&gt;&lt;code&gt;"&lt;/code&gt;&lt;code&gt;&amp;gt;&amp;lt;/user-list&amp;gt;&lt;/code&gt; . The &lt;code&gt;v-bind:&lt;/code&gt; prefix tells Vue that we want to dynamically bind the &lt;code&gt;users&lt;/code&gt; props to a variable, and not pass in a string literal directly.  &lt;/p&gt;

&lt;p&gt;Right now, we have this as our app source code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;user-list&lt;/span&gt; &lt;span class="na"&gt;v-bind:users=&lt;/span&gt;&lt;span class="s"&gt;"allUsers"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/user-list&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userList&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;template&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;div&amp;gt;{{users}}&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

      &lt;span class="p"&gt;});&lt;/span&gt;

       &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
           &lt;span class="na"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

           &lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="na"&gt;allUsers&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="na"&gt;about&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Really nice guy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane Dean&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loves eggs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Clark Kent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not your everyday reporter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
              &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember we said earlier that we want our component to be able to list all users passed to it. To do this, we need to perform List Rendering, using the &lt;code&gt;v-for&lt;/code&gt; directive. The directive is used to render a list of items based on an array. &lt;br&gt;
The syntax is like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"item in items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, items is an array and item is an alias for the current array item being iterated upon. Armed with the knowledge of list rendering, let’s extend our user-list component to iterate over all users. To do this, we replace our template with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
        &amp;lt;ul&amp;gt;
          &amp;lt;li v-for="user in users"&amp;gt;
            {{user.name}}
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you are not familiar, the back-ticks are called template literals in modern JavaScript, they allow us to have multi-line statements, like the one above. You can learn more about template literals &lt;a href="https://dev.to/sarah_chima/an-introduction-to-es6-template-literals-94l"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we have done above is to define a base &lt;code&gt;ul&lt;/code&gt; element and then, iterate through, and dynamically create all the &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; tags in the element using the &lt;code&gt;v-for&lt;/code&gt; list rendering directive.  If you run the code we currently have, this should be your output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310219%2F3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310219%2F3.png" alt="Looping through the user object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling the Click Event
&lt;/h2&gt;

&lt;p&gt;Because we want our components to be re-usable, we will not handle the click event inside the component, instead, we will return the event to the parent component, which will use passed in the payload to perform any action it pleases. The advantage of this is that we can use the same component for many different purposes. &lt;/p&gt;

&lt;p&gt;We will do this by making the &lt;code&gt;user-list&lt;/code&gt; component emit an event when an item is clicked, and we will be handling this event on the parent component. &lt;/p&gt;

&lt;p&gt;Let’s add an onclick listener to the &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; element, we do this in Vue by adding the &lt;code&gt;@click&lt;/code&gt; attribute. This click event will call an internal method, and pass the user’s &lt;code&gt;about&lt;/code&gt; attribute to the method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"user in users"&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"emitClicked(user.about)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  {{user.name}}
&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see above, that there is a method passed to the click handler, called the emitClicked method, we will define this method by adding the methods attribute to our Vue component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;methods&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;emitClicked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;$emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item-clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method emits an event, with a payload, which the parent can listen for, and use for operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Listening for Events
&lt;/h2&gt;

&lt;p&gt;The easiest way to listen for an event in a parent component is by using the &lt;code&gt;v-on&lt;/code&gt; attribute. Remember in the child component, we emitted an &lt;code&gt;item-clicked&lt;/code&gt; event, so we can easily listen for the event by adding the &lt;code&gt;v-on:item-clicked&lt;/code&gt; attribute to the &lt;code&gt;&amp;lt;user-list&amp;gt;&lt;/code&gt; HTML tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;user-list&lt;/span&gt; &lt;span class="na"&gt;v-bind:users=&lt;/span&gt;&lt;span class="s"&gt;"allUsers"&lt;/span&gt; &lt;span class="na"&gt;v-on:item-clicked=&lt;/span&gt;&lt;span class="s"&gt;"alertData"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/user-list&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the code above, we can see that there is a new method called &lt;code&gt;alertData&lt;/code&gt;, This method is what handles the payload(data) passed out from the child component when it emitted the event. &lt;/p&gt;

&lt;p&gt;We will define the &lt;code&gt;alertData&lt;/code&gt; method inside the main component by adding the methods attribute as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
 &lt;span class="nx"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alertData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method simply uses the native alert method to display the data that has been passed from the child component.  &lt;/p&gt;

&lt;p&gt;Right Now our entire code should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
    &lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;user-list&lt;/span&gt; &lt;span class="na"&gt;v-bind:users=&lt;/span&gt;&lt;span class="s"&gt;"allUsers"&lt;/span&gt; &lt;span class="na"&gt;v-on:item-clicked=&lt;/span&gt;&lt;span class="s"&gt;"alertData"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/user-list&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userList&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;template&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
            &amp;lt;ul&amp;gt;
              &amp;lt;li v-for="user in users" @click="emitClicked(user.about)"&amp;gt;
                {{user.name}}
              &amp;lt;/li&amp;gt;
            &amp;lt;/ul&amp;gt;
          `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

          &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;

          &lt;span class="na"&gt;methods&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;emitClicked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

              &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;$emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item-clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="p"&gt;});&lt;/span&gt;

           &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
               &lt;span class="na"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

               &lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="na"&gt;allUsers&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                      &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="na"&gt;about&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Really nice guy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                      &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane Dean&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loves eggs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                      &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Clark Kent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not your everyday reporter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                  &lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
               &lt;span class="p"&gt;},&lt;/span&gt;

               &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
               &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="nf"&gt;alertData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="p"&gt;}&lt;/span&gt;
               &lt;span class="p"&gt;}&lt;/span&gt;

           &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310221%2F4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fddbm6rq61%2Fimage%2Fupload%2Fv1528310221%2F4.png" alt="Our Final Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The re-usability of this component lies in the fact that the &lt;code&gt;v-on:item-clicked&lt;/code&gt; can accept different methods, and produce different outputs, hence, the &lt;code&gt;user-list&lt;/code&gt;  component can be reused severally across the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Components can be very useful in separating concerns for our front-end application and breaking it up into smaller logical bits. As a rule of thumb, components should perform a singular function, to enable maximum re-usability. In this article, we have been able to explore how to create a component, and pass data between the component and its parent.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>components</category>
    </item>
    <item>
      <title>This is Real Life</title>
      <dc:creator>Friday Godswill</dc:creator>
      <pubDate>Wed, 21 Mar 2018 22:17:58 +0000</pubDate>
      <link>https://dev.to/faradayyg/this-is-real-life-2ki7</link>
      <guid>https://dev.to/faradayyg/this-is-real-life-2ki7</guid>
      <description>&lt;p&gt;You've probably already Mastered React, I bet you've mastered CSS grid and Flexbox too, and oh you know enough of VueJS and Angular to get by. &lt;/p&gt;

&lt;p&gt;I bet you're proud of yourself. &lt;/p&gt;

&lt;p&gt;This is real life, bro.&lt;/p&gt;

&lt;p&gt;In real life, people don't update their browsers, and others still use Opera Mini and, you guessed it, CSS grid will not work.&lt;/p&gt;

&lt;p&gt;This is real life, and you will be forced to use Jquery over React.&lt;/p&gt;

&lt;p&gt;This is real life, and in real life, You can't just dump your database tables and start afresh. Well, you can, but I cannot guarantee what will happen after you do.&lt;/p&gt;

&lt;p&gt;This is real life, even if your internal APIs are RESTful, I bet you will have to work with some external SOAP APIs, tough luck.&lt;/p&gt;

&lt;p&gt;I bet you hate to look at one thousand-line functions, I do too. I bet, also that you are itching to refactor. But can you, really? I mean, this is real life and so many other things depend on this one function, something could break you know. But hey, have a crack at it if you would.&lt;/p&gt;

&lt;p&gt;We all love to build new products instead of debugging some old code written by some guy you never knew, who returns plain HTML from an API endpoint. But this is the real world, and you have to maintain that cash cow. &lt;/p&gt;

&lt;p&gt;In the end, it's not about the languages you know, it's not about how much "ninjutsu" you can do with this shiny new framework. In the end, it's the basics that count.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What are JavaScript Callbacks Anyway?</title>
      <dc:creator>Friday Godswill</dc:creator>
      <pubDate>Thu, 19 Oct 2017 14:23:36 +0000</pubDate>
      <link>https://dev.to/faradayyg/understanding-javascript-callbacks-58i</link>
      <guid>https://dev.to/faradayyg/understanding-javascript-callbacks-58i</guid>
      <description>&lt;p&gt;Imagine you have a car, and the car breaks down, but this car is really important to you because you need it to go to work the next day. So you call your friend John, John is an auto-repair man, John tows your car to his workshop. But before he leaves, you ask him to call you once he is done with the repairs (again, because the car is so important). John eventually finishes effecting repairs in 2 hours, then calls you. &lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing JavaScript Callbacks.
&lt;/h2&gt;

&lt;p&gt;Say the above example is a JavaScript program, with &lt;code&gt;repairCar()&lt;/code&gt; being a function within the program which asks John to come tow your car, the instruction issued to him to call you when he is done with the repairs can be likened to a callback. How? because you simply issued a sub instruction to John, asking to be notified when repairs are done.&lt;br&gt;
This callback, is another instruction within an existing instruction, to be carried out after the main instruction. 😅&lt;/p&gt;

&lt;p&gt;A callback function is defined as function, say F that is passed to another function, G, causing F to be executed inside G. Nerdy stuff 🤓&lt;/p&gt;

&lt;p&gt;Using the example of the default javascript &lt;code&gt;setTimeout()&lt;/code&gt; function, we will examine how callbacks are used. the &lt;code&gt;setTimeout()&lt;/code&gt; function takes two parameters, a callback, and the time (in milliseconds) before the callback is executed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout( function(){console.log('callback run')}, 3000 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the code block above demonstrates the use of a callback within the &lt;code&gt;setTimeout()&lt;/code&gt; function. Analyzing this properly, we can see that only two parameters were passed to the &lt;code&gt;setTimeout()&lt;/code&gt; Function: the callback, and the time in milliseconds.&lt;/p&gt;

&lt;p&gt;Yeah, callbacks are very cool and all, but can I define my own functions that need callbacks?&lt;br&gt;
Sure, sure you can. I will jump straight into an example.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function needCallback(parameter, callBackEntry = function(){}){

        //perform computation with parammeter one

        const result = parameter * 100;

        //execute callback function
        callBackEntry(result);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let me explain, in the code block above, we simply defined a function that takes two parameters, the second, being a callback function.&lt;br&gt;
Within the body of the function, we performed a computation with the first parameter, and passed its result to the callback function, which is the second parameter. &lt;/p&gt;

&lt;p&gt;To use the above function, we simply need to do this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;needCallback(20, function(result){
console.log(result)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can see clearly, that the first parameter is the number used for the computation, and the second parameter is the callback function. Pretty neat right?&lt;/p&gt;

&lt;p&gt;Callbacks are very important, especially for processes that are dependent on the results of other computations in their flow of execution, say maybe a network call.&lt;/p&gt;

&lt;p&gt;But what if I have a function that depends on another function, which depends on a third function to work? Fret not,  JS got you handled. &lt;/p&gt;

&lt;h2&gt;
  
  
  Enter: Callback Chaining.
&lt;/h2&gt;

&lt;p&gt;Callbacks can be chained so that functions are executed in a chronological order. To achieve this, one could nest callbacks inside other callbacks. take the example below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//call functions in order

first('hello', function(){
        second('my',function(){
                third('friend',endFunction)
        })
})

//Define the functions used below:

// the first function to be executed

function first(data,cb){
        console.log(data);
        cb();
}

//second function to be executed

function second(data,cb){
        console.log(data);
        cb();
}

//third function to be executed

function third(data, cb){
        console.log(data);
        cb();
}

//Last function to be executed

function endFunction(){
        console.log('ended')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Pretty confusing, I know, but what has happened here is that the &lt;code&gt;endFunction()&lt;/code&gt; was chained to the &lt;code&gt;third()&lt;/code&gt; which was chained to the &lt;code&gt;second()&lt;/code&gt; which was eventually chained to the &lt;code&gt;first()&lt;/code&gt;. This ensures that the functions are executed in the order which they have been written. &lt;/p&gt;

&lt;p&gt;Thankfully there are not too many cases where you’d need to do this, but if you absolutely have to, there is another concept called Promises which simplifies the whole chaining process. &lt;/p&gt;

&lt;p&gt;Now that you understand callbacks, (hopefully) let's write out the example I gave previously as a JavaScript program.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function repairCar(callback = function(){}){
    //do the car repairs
    callback();
}

function callMe()
{
    //do the function that calls me
}

//car breaks down, so You call John

repairCar(function(){
    callMe
})

//John repairs the car and then calls me.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is in no way an exhaustive tutorial on callbacks, but I hope it is enough to get you started.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callbacks</category>
    </item>
  </channel>
</rss>
