<?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: Aman Kumar</title>
    <description>The latest articles on DEV Community by Aman Kumar (@dustyworld666).</description>
    <link>https://dev.to/dustyworld666</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%2F682715%2F55738c47-8374-4306-9c73-c159c89800aa.jpg</url>
      <title>DEV Community: Aman Kumar</title>
      <link>https://dev.to/dustyworld666</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dustyworld666"/>
    <language>en</language>
    <item>
      <title>A beginner's guide to VueJS</title>
      <dc:creator>Aman Kumar</dc:creator>
      <pubDate>Fri, 16 Sep 2022 08:36:56 +0000</pubDate>
      <link>https://dev.to/dustyworld666/a-beginners-guide-to-vuejs-3oa1</link>
      <guid>https://dev.to/dustyworld666/a-beginners-guide-to-vuejs-3oa1</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Hey there, everybody. In this article, I'll be showing you how you can start your journey with VueJS, a popular front-end framework. This guide will help you understand the basics of VueJS to a level where you'll be able to carry on with it and build some awesome projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is VueJS?
&lt;/h2&gt;

&lt;p&gt;If you go to their &lt;a href="https://vuejs.org/" rel="noopener noreferrer"&gt;official&lt;/a&gt; website, it is written that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vue (pronounced /vjuː/, like view) is a JavaScript framework for building user interfaces. It builds on top of standard HTML, CSS, and JavaScript, and provides a declarative and component-based programming model that helps you efficiently develop user interfaces, be it simple or complex.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A few things we understand from it are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's a front-end framework like React or Angular.&lt;/li&gt;
&lt;li&gt;It provides a declarative and component-based programming model. Here declarative means an element pulls down information only from state and props. Component-based means code can be divided into components and reused instead of writing the same logic again and again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;VueJS uses two API styles, Options, and Composition API. In options API, we define a component's logic using an object of options such as data, methods and mounted. In composition API, we use imported API functions like &lt;code&gt;ref&lt;/code&gt; and &lt;code&gt;onMounted&lt;/code&gt;.&lt;br&gt;
For this article, I'll be using the options API only. Feel free to explore the &lt;a href="https://vuejs.org/api/composition-api-setup.html#composition-api-setup" rel="noopener noreferrer"&gt;docs&lt;/a&gt; for the composition API.&lt;/p&gt;

&lt;p&gt;So without further ado, let's get started. &lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Surprisingly enough, all you need for this tutorial is a simple HTML file! Simply create an HTML file and add the below code to it.&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&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&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;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&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;h1&amp;gt;&lt;/span&gt;Hello From Vue&lt;span class="nt"&gt;&amp;lt;/h1&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;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/vue@3/dist/vue.global.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;script&amp;gt;&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&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="nf"&gt;createApp&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;
            &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mount&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;It's pretty straightforward. All we're doing is adding a VueJS CDN and mounting it on div with id "app". It means that we can write vuejs-like syntax which we'll discuss in the next part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declarative Rendering
&lt;/h2&gt;

&lt;p&gt;As described earlier, VueJS is built on top of simple javascript, HTML, and CSS, which allows us to declaratively describe the HTML output through the javascript state. Let's create our state and try to render it on HTML. Refactor the &lt;code&gt;const app = Vue.createApp({})&lt;/code&gt; line so that it looks like 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&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="nf"&gt;createApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;my_message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&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;Here we're defining &lt;code&gt;data&lt;/code&gt;, which is a special method that returns an object with states. For this example, we've defined a state variable &lt;code&gt;my_message&lt;/code&gt; inside the &lt;code&gt;data&lt;/code&gt;. Now inside the &lt;code&gt;app&lt;/code&gt; div, create a &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag and add &lt;code&gt;{{my_message}}&lt;/code&gt; to it. Try running the file on the browser, you be getting &lt;strong&gt;Hello World&lt;/strong&gt; as the output.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactivity
&lt;/h2&gt;

&lt;p&gt;Now, let's say I have a button and on clicking it I want to change &lt;code&gt;my_message&lt;/code&gt; to &lt;em&gt;Hello from Vue!&lt;/em&gt;. For this, we'll create a method to change the text. In VueJS, we define methods in the &lt;strong&gt;methods: {}&lt;/strong&gt; object. Create a &lt;code&gt;methods&lt;/code&gt; object below the &lt;em&gt;data&lt;/em&gt; method and define a &lt;strong&gt;changeText&lt;/strong&gt; method. Also, add a button inside HTML just like mentioned below:&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="c"&gt;&amp;lt;!-- ANYWHERE INSIDE div with id app --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;v-on:click=&lt;/span&gt;&lt;span class="s"&gt;"changeText"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Change Text&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BELOW data METHOD&lt;/span&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;changeText&lt;/span&gt;&lt;span class="p"&gt;()&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="nx"&gt;my_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello from 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;Now try opening your browser and click on the button. The output should change to &lt;em&gt;Hello from Vue!&lt;/em&gt;. Now those coming from React might feel a bit weird because in react we're used to defining both state and a function responsible for changing the current state. In Vue, we're directly changing our state. This is possible because of reactivity which means that Vue automatically tracks JavaScript state changes and efficiently updates the DOM when changes happen. This, along with the declarative rendering, constitutes the key features of VueJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Directives
&lt;/h2&gt;

&lt;p&gt;In the previous example, we've defined a &lt;code&gt;v-on:click&lt;/code&gt; attribute on the button. This is known as a directive. They're special attributes with the prefix &lt;code&gt;v-&lt;/code&gt;. VueJS provides a number of built-in directives. Some of the most commonly used ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;v-on: Attaches an event listener. One can also use &lt;code&gt;@&amp;lt;event-name&amp;gt;&lt;/code&gt; as a shorthand syntax. &lt;/li&gt;
&lt;li&gt;v-if/v-else-if/v-else: Applies conditional rendering. &lt;/li&gt;
&lt;li&gt;v-show: Same as v-if, however instead of add or removing the element altogether, it just hides the element (display: hidden). &lt;/li&gt;
&lt;li&gt;v-for="elem in arr"/key="": Iterates over the provided array and renders elements as many times as the length of the array. It requires the key attribute to be used, to uniquely identify the elements.&lt;/li&gt;
&lt;li&gt;v-bind:: Dynamically binds the attribute to a state. Shorthand property &lt;code&gt;:&amp;lt;attribute&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can check out other directives in the &lt;a href="https://vuejs.org/api/built-in-directives.html#built-in-directives" rel="noopener noreferrer"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's try to use some of the directives mentioned above. Suppose we want to render a list of items. Try adding an array with a list of items in it, in the state. Now, use &lt;code&gt;v-for&lt;/code&gt; attribute to render it on the dom, just like mentioned below.&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;ul&amp;gt;&lt;/span&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, idx) in items"&lt;/span&gt; &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"idx"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Item number {{idx + 1}} - {{item}}
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In order to provide a dynamic key, you can bind it using &lt;code&gt;v-bind:key&lt;/code&gt; or &lt;code&gt;:key&lt;/code&gt; for short. Try adding v-if and v-show as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lifecycle Methods
&lt;/h2&gt;

&lt;p&gt;Each vue component goes through a series of phases, mounting, updating, and un-mounting. Vue provides some special methods, a.k.a Lifecycle methods, that you can use whenever necessary. The most common use case includes fetching data from the server, logging, etc. Some commonly used methods are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;created: Called after the instance has finished processing all state-related options.&lt;/li&gt;
&lt;li&gt;mounted: Called after the component is rendered on the dom.&lt;/li&gt;
&lt;li&gt;updated: Called after there are some changes in the dom.&lt;/li&gt;
&lt;li&gt;unmounted: Called after the component is removed from the dom.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;created&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`App is created`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="nf"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`App is mounted`&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;Initially, you'll see &lt;code&gt;App is created&lt;/code&gt; inside the console. Now click the &lt;code&gt;Change Text&lt;/code&gt; button we created earlier. You should see &lt;code&gt;App is mounted&lt;/code&gt; in the console this time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Components
&lt;/h2&gt;

&lt;p&gt;In the beginning, I told you that VueJS provides &lt;strong&gt;component-based programming model&lt;/strong&gt;. That means we can create our custom components. Let's try creating one. Add this to your code right before the &lt;code&gt;app.mount("#app")&lt;/code&gt; line:&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;custom-heading&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;h1&amp;gt;Hello From Custom Component&amp;lt;/h1&amp;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;Here we're creating a &lt;code&gt;custom-heading&lt;/code&gt; component, and inside it, we're just defining a simple h1 tag. Also, in order to use the custom components, you need to register them inside the &lt;code&gt;components&lt;/code&gt; array. Add the below code right below the &lt;code&gt;updated&lt;/code&gt; method:&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;components&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="s2"&gt;custom-heading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try adding this custom component in your main html. You can use this anywhere in your code 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;custom-heading&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far so good. One thing we're missing here is how can we pass data to the child components. Let's try implementing that. Suppose we want to render our own custom message in the child component. For this, we need to accept the props in the child components. Add the props array below the &lt;code&gt;template&lt;/code&gt; and instead of a hard-coded message inside the h1 tag, use the variable inside the props.&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="c"&gt;&amp;lt;!-- INSIDE TEMPLATE --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{{msg}}&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BELOW template&lt;/span&gt;
&lt;span class="nx"&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="s2"&gt;msg&lt;/span&gt;&lt;span class="dl"&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 the parent component, pass msg attribute to &lt;code&gt;&amp;lt;custom-heading /&amp;gt;&lt;/code&gt;.&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;custom-heading&lt;/span&gt; &lt;span class="na"&gt;msg=&lt;/span&gt;&lt;span class="s"&gt;"Hello From Parent Component"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also give it dynamically using the &lt;code&gt;v-bind:&amp;lt;attribute&amp;gt;&lt;/code&gt; directive.&lt;/p&gt;

&lt;p&gt;You can also emit events from the child component to the parent component. In the parent component, you can listen for events using &lt;code&gt;v-on&lt;/code&gt; directive mentioned below.&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;custom-component&lt;/span&gt; &lt;span class="na"&gt;v-on:response=&lt;/span&gt;&lt;span class="s"&gt;"(text) =&amp;gt; my_message = text"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this will do is, it will keep on listening for a &lt;code&gt;response&lt;/code&gt; event. When that happens, it will set the my_message variable to whatever we get from the child component. This is one of the main advantages of using VueJS overreact. In react, if you need to get anything from the child component, you first need to uplift the state and then pass on the callback function to the child component. This allows getting data from child components in a much simpler fashion.&lt;/p&gt;

&lt;p&gt;Now in order to emit a response add the response in the emits array and emit the response event like 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="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="s2"&gt;response&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;your message&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use it anywhere, be it a button event or inside lifecycle hooks. I've used it inside the &lt;code&gt;created&lt;/code&gt; method like 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;created&lt;/span&gt;&lt;span class="p"&gt;()&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="s2"&gt;response&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Howdy&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should change our &lt;code&gt;my_message&lt;/code&gt; variable to &lt;em&gt;Howdy&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope I've covered enough to get you started with your next major project. Feel free to write your thoughts about the article in the comments section. &lt;/p&gt;

&lt;p&gt;Thank you for reading. I hope you enjoyed it :).&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Dockerize Your First Nodejs App</title>
      <dc:creator>Aman Kumar</dc:creator>
      <pubDate>Mon, 12 Sep 2022 05:18:06 +0000</pubDate>
      <link>https://dev.to/dustyworld666/dockerize-your-first-nodejs-app-5hia</link>
      <guid>https://dev.to/dustyworld666/dockerize-your-first-nodejs-app-5hia</guid>
      <description>&lt;p&gt;I've used docker a lot of times but containerizing an app felt a lot harder because of all those dockerfiles and docker compose instructions. In this article, I'll try to show you how you can containerize your NodeJS app easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is docker?
&lt;/h2&gt;

&lt;p&gt;Now first things first, let me tell you what is docker and why one should use it. Docker is a software that uses OS-level virtualization to deliver software in packages called containers. In short, it's just an application that kind of gives you a virtual machine that you can deploy elsewhere, but the main difference between a virtual machine and a docker container is that a virtual machine is a full-fledged operating system running on a virtual environment, which means docker container will have a host OS and a guest OS for every virtual machine. On the other hand, docker containers are just mini virtual machines that share a host OS kernel, which makes them very lightweight. &lt;/p&gt;

&lt;p&gt;Now, why one should use it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fklyqg8ys4goyg3cm7ws0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fklyqg8ys4goyg3cm7ws0.png" alt="Docker meme" width="422" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might've come across this meme, and that's the exact reason why we use docker. It helps you create applications without worrying about whether it's gonna work in a production environment or not. As long as it works inside the container, you can be sure that it will work anywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Now let's start by creating our first container. For this article, we'll be using a simple nodejs authentication application with MySQL as the database. Here's how the app directory looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkpmxbr7s6svqc0rqtjo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkpmxbr7s6svqc0rqtjo.png" alt="File directory" width="279" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the &lt;strong&gt;docker-compose.yml&lt;/strong&gt; and &lt;strong&gt;Dockerfile&lt;/strong&gt; in the end? These are the two main superheroes of this article :).&lt;/p&gt;

&lt;p&gt;Dockerfile is a set of instructions to build images. You can think of it as manual instructions you might give if you're not using docker like running npm packages, running node app, etc.&lt;/p&gt;

&lt;p&gt;Docker-compose.yml is used to run images. It specifies all the services, environment variables, etc. If you're familiar with the docker run command, it's the same thing only. However, instead of executing the docker run command every time in the terminal, you use docker-compose.yml.&lt;/p&gt;

&lt;p&gt;Now let's start by creating our Dockerfile. Create a file with the name &lt;strong&gt;Dockerfile&lt;/strong&gt; in the root directory of your app and paste the below content into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:14&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json .&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; yarn.lock .&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["yarn", "start"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me go through each of these one-by-one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;FROM node:14&lt;/strong&gt;: This describes that your parent image is node version 14. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WORKDIR /app&lt;/strong&gt;: This states that your working directory will be /app &lt;em&gt;inside the container.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;COPY package.json .&lt;/strong&gt;: It's pretty straightforward and it will just copy package.json to the working directory. The same goes for yarn.lock as well as &lt;strong&gt;COPY. .&lt;/strong&gt; where we're giving the instructions to copy everything from the current directory to the working directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RUN yarn install&lt;/strong&gt;: The RUN instruction is used to run a Linux command inside your app. Here we're giving the instructions to execute the &lt;em&gt;yarn install&lt;/em&gt; command (installs nodejs packages). If you use npm then you'll run &lt;strong&gt;npm install&lt;/strong&gt; instead of &lt;strong&gt;yarn install&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CMD ["yarn", "start"]&lt;/strong&gt;: The CMD instruction specifies the default program that will be executed once the container runs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So far so good. Let's create our docker-compose.yml file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2"&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mysql_container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;any custom name&amp;gt;&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mysql&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;mysql-root-password&amp;gt;&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;mysql-root-database&amp;gt;&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;mysql_db:/var/lib/mysql&lt;/span&gt;

  &lt;span class="c1"&gt;# API&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;3000:3000&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;mysql_container&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mysql_db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Make sure that you're indenting your docker-compose.yml properly. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;version: "2"&lt;/strong&gt;: States the docker version you're using. &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;services&lt;/strong&gt;: It's the place where we define all the services and their configuration required for your node app to work. As stated earlier, for this article, I'm using a simple node authentication app with MySQL as the database. So here I've defined two services &lt;em&gt;mysql_container&lt;/em&gt; and &lt;em&gt;app&lt;/em&gt;. Let's go through them one by one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;mysql_container&lt;/strong&gt;: Here I've defined the database. 

&lt;ul&gt;
&lt;li&gt;container_name: You can optionally give the container_name. It's just a custom name for your container.
&lt;/li&gt;
&lt;li&gt;image: It states which image your service uses. In this case, I'm using MySQL so I've written that over there. If you're using any other database, say for example PostgreSQL or mongo, you can use that as well.
&lt;/li&gt;
&lt;li&gt;environment: Here you define the environment variables that you put in the .env file. Different images require you to define different environment variables, so if you're using any other database, simply check the docs.&lt;/li&gt;
&lt;li&gt;restart always: It makes sure that your container restarts when docker restarts or the app crashes.&lt;/li&gt;
&lt;li&gt;volumes: Here you define the volumes your app is using. By default data in the container will remain until you stop it. After that, the data will be gone. To avoid that you use volumes. Here I've given the path as /var/lib/mysql which is the default DB location in the case of MySQL.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;app&lt;/strong&gt;: This is our main node app.

&lt;ul&gt;
&lt;li&gt;build: We've previously used dockerfile to give build instructions for our app. Here we're giving the path location to dockerfile. &lt;/li&gt;
&lt;li&gt;ports: It will map the host port to the container port. In short, it will expose port 3000 from the container. &lt;/li&gt;
&lt;li&gt;depends_on: Since we require a MySQL database before we run our app, here we will instruct that until mysql_container is not running don't run the app.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;volumes&lt;/strong&gt;: Here we defined all the volumes we're using. &lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So far so good. The last step is to run this command &lt;code&gt;sudo docker-compose -f docker-compose.yml up&lt;/code&gt;. This will run our containerized application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Docker is one of the coolest things I've learned so far. In the beginning, it might be somewhat difficult to grasp all the concepts, but once you learn it'll make your life a lot easier.&lt;/p&gt;

&lt;p&gt;Thank you for reading. I hope you enjoyed this article :). &lt;/p&gt;

</description>
      <category>docker</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>node</category>
    </item>
  </channel>
</rss>
