<?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: Ehab-24</title>
    <description>The latest articles on DEV Community by Ehab-24 (@ehab24).</description>
    <link>https://dev.to/ehab24</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%2F1055647%2Fa099e142-79b5-4cc5-9108-12ad352538f6.png</url>
      <title>DEV Community: Ehab-24</title>
      <link>https://dev.to/ehab24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ehab24"/>
    <language>en</language>
    <item>
      <title>Vue.js for beginners</title>
      <dc:creator>Ehab-24</dc:creator>
      <pubDate>Tue, 04 Apr 2023 10:00:39 +0000</pubDate>
      <link>https://dev.to/ehab24/vuejs-for-beginners-1oee</link>
      <guid>https://dev.to/ehab24/vuejs-for-beginners-1oee</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;This tutorial is a starting point for anyone getting started with Vuejs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;You will need a nodding acquaintance with the basics of Vue such as &lt;em&gt;v-on&lt;/em&gt;, &lt;em&gt;v-bind&lt;/em&gt;, &lt;em&gt;refs&lt;/em&gt; etc. Please refer to the &lt;a href="https://vuejs.org/guide/introduction.html"&gt;official documentation&lt;/a&gt; for Vue.js if you feel unfimiliar with a terminology at any point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Topics
&lt;/h3&gt;

&lt;p&gt;Following are the topics we will be taking a look at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single File Components&lt;/li&gt;
&lt;li&gt;Props and  Events&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;li&gt;Composables&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Source Code
&lt;/h3&gt;

&lt;p&gt;To follow along with the tutorial, you can find the source code &lt;a href=""&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Setup
&lt;/h1&gt;

&lt;p&gt;For a better developer experience, we will be using &lt;strong&gt;vite&lt;/strong&gt; along with &lt;strong&gt;tailwindcss&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to install?
&lt;/h3&gt;

&lt;p&gt;To get started with vite and tailwind, simply follow the &lt;a href="https://tailwindcss.com/docs/guides/vite#vue"&gt;official guide&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cleanup
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to &lt;code&gt;my-project/src&lt;/code&gt; and remove the everything except for &lt;code&gt;main.js&lt;/code&gt;, &lt;code&gt;style.css&lt;/code&gt; and &lt;code&gt;App.vue&lt;/code&gt; files. &lt;/li&gt;
&lt;li&gt;Remove everything in &lt;code&gt;App.vue&lt;/code&gt;, we will come back to it in a minute.&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Single File Components
&lt;/h1&gt;

&lt;p&gt;A &lt;a href="https://vuejs.org/guide/scaling-up/sfc.html"&gt;single file component&lt;/a&gt; (SFC) is a special file format that allows us to encapsulate the template, logic and styling of a Vue component in a single file. These files are named as &lt;code&gt;*.vue&lt;/code&gt;, where &lt;code&gt;*&lt;/code&gt; is the name of component in &lt;em&gt;UpperCamelCase&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constraints
&lt;/h3&gt;

&lt;p&gt;A Vue SFC must have atleast one &lt;code&gt;&amp;lt;sricpt&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt; tag. However, Vue will give a &lt;strong&gt;warning&lt;/strong&gt; if no &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt; tag is provided to an SFC.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Create a new folder in the &lt;code&gt;src&lt;/code&gt; directory and name it &lt;code&gt;components&lt;/code&gt;. Create a new file in &lt;code&gt;src/components&lt;/code&gt; with the name &lt;code&gt;Oven.vue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nBVQAey7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a22zeg7gcgk6pngqygmk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nBVQAey7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a22zeg7gcgk6pngqygmk.png" alt="oven component" width="880" height="965"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  What is happening?
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;We bind the &lt;code&gt;disabled&lt;/code&gt; property of &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; to the &lt;code&gt;isBusy&lt;/code&gt; ref. Any time the value of &lt;code&gt;isBusy&lt;/code&gt; changes, our button will listen to the change and re-render (if needed).&lt;/li&gt;
&lt;li&gt;We also provide a listener &lt;code&gt;v-on:click&lt;/code&gt; or &lt;code&gt;@click&lt;/code&gt; to the &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; component and assign it a &lt;em&gt;handler&lt;/em&gt; function. This &lt;em&gt;handler&lt;/em&gt; is executed every time the button is pressed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Add the oven to App.vue and test it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bM54T6sB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1r64950vxsl24fokfbu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bM54T6sB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1r64950vxsl24fokfbu.png" alt="App component" width="880" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--USE4V6xZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47q1ebvqpz0f1d42hwna.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--USE4V6xZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47q1ebvqpz0f1d42hwna.png" alt="Program Run" width="880" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Views
&lt;/h3&gt;

&lt;p&gt;Views in Vue are exactly the same as Vue components. The components that act as &lt;em&gt;views&lt;/em&gt; or &lt;em&gt;pages&lt;/em&gt; for routing are referred to as views.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Create a new folder in the &lt;code&gt;src&lt;/code&gt; directory and name it &lt;code&gt;views&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add a new file in &lt;code&gt;src/views&lt;/code&gt; with the name &lt;code&gt;Kitchen1.vue&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I-FsxtIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kydtuy2a3ksyyu73nehi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I-FsxtIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kydtuy2a3ksyyu73nehi.png" alt="Kitchen1 view component" width="880" height="629"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our kitchen only contains a single oven for now.&lt;/p&gt;

&lt;p&gt;Let's also update the &lt;code&gt;App.vue&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P-yT2xaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye2hshoe155iq7wrgr5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P-yT2xaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye2hshoe155iq7wrgr5l.png" alt="Add Kitchen1 to App" width="880" height="666"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aO0Egwem--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/370rtaotjl6v78uu7unp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aO0Egwem--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/370rtaotjl6v78uu7unp.png" alt="Program Run" width="880" height="288"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Props
&lt;/h1&gt;

&lt;p&gt;Props are attributes that are passed to a Vue SFC via the parent component. They can be declared using the &lt;code&gt;defineProps&lt;/code&gt; macro when using &lt;code&gt;&amp;lt;script setup&amp;gt;&lt;/code&gt;. This function returns an object containing all the passed props.&lt;/p&gt;

&lt;h3&gt;
  
  
  Receiving props
&lt;/h3&gt;

&lt;p&gt;Let's change the oven component to bake whatever we want instead of just a cake.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zCQMKnrY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rnnh1vcr0q2yxyoe96v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zCQMKnrY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rnnh1vcr0q2yxyoe96v.png" alt="Add props to Oven component" width="880" height="1022"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will also need to change the &lt;code&gt;bakeACake&lt;/code&gt; function and &lt;code&gt;prompt&lt;/code&gt; logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HjFFa-15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lkpu8axwdpybomnh85l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HjFFa-15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lkpu8axwdpybomnh85l.png" alt="Add logic to Oven component" width="880" height="742"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing props
&lt;/h3&gt;

&lt;p&gt;I say we bake a banana bread this time.&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;views/Kitchen1.vue&lt;/code&gt; and pass &lt;code&gt;food&lt;/code&gt; and &lt;code&gt;duration&lt;/code&gt; props to the oven.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SDg8qJQh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ailzaphos70nft2uclxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SDg8qJQh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ailzaphos70nft2uclxk.png" alt="Passing props to component" width="880" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that we pass &lt;code&gt;banana bread&lt;/code&gt; as a static string but &lt;em&gt;bind&lt;/em&gt; the &lt;code&gt;duration&lt;/code&gt; property to &lt;code&gt;3000&lt;/code&gt; even though it is static. This is because we need to tell Vue that this a Javascript expression (a number in our case) instead of a string.&lt;/p&gt;

&lt;p&gt;Let's see how it works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7rynBZ_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7dgs4f8drugefr7muqj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7rynBZ_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7dgs4f8drugefr7muqj.png" alt="Program Run" width="880" height="250"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Events
&lt;/h1&gt;

&lt;p&gt;What if our kitchen had multiple ovens to bake different food items and we wanted to keep track of all the items currently being baked?&lt;/p&gt;

&lt;h5&gt;
  
  
  Solution
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Create multiple ovens.&lt;/li&gt;
&lt;li&gt;Add to 'currentlyBaking' when an oven is started.&lt;/li&gt;
&lt;li&gt;Listen to the event when a food item is baked and remove it from the &lt;code&gt;currentlyBaking&lt;/code&gt; list.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Create multiple ovens
&lt;/h5&gt;

&lt;p&gt;Navigate to &lt;code&gt;views/Kitchen1.vue&lt;/code&gt; and initialize a constant list &lt;code&gt;ovens&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6aC6xJGF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na5o061ps1blga4lwg10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6aC6xJGF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na5o061ps1blga4lwg10.png" alt="Create ovens list" width="880" height="985"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now replace the &lt;code&gt;&amp;lt;Oven /&amp;gt;&lt;/code&gt; tag with a &lt;code&gt;v-for&lt;/code&gt; directive that will render the &lt;code&gt;ovens&lt;/code&gt; list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--li2c3k88--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55s40tz08kjxdzttsfid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--li2c3k88--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55s40tz08kjxdzttsfid.png" alt="Using v-for to render ovens" width="880" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PZ8OxzLC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23y79hhkk6zcahumr4ab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PZ8OxzLC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23y79hhkk6zcahumr4ab.png" alt="Program Run" width="880" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Emitting events
&lt;/h5&gt;

&lt;p&gt;We need our oven to tell the kitchen when it starts baking an item as well as when it finishes. To accomplish this task, we will simply emit events from the oven component.&lt;br&gt;
Navigate to &lt;code&gt;components/Oven.vue&lt;/code&gt; and define &lt;em&gt;start&lt;/em&gt; and &lt;em&gt;baked&lt;/em&gt; events using the &lt;code&gt;defineEmits&lt;/code&gt; macro.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DcA3JiGY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n51y5yf024vi05tbynv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DcA3JiGY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n51y5yf024vi05tbynv.png" alt="defining emits for oven" width="880" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's also emit these events using the &lt;code&gt;emit&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qH6ncduT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txakwf4x2i6v3m6o1sjh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qH6ncduT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txakwf4x2i6v3m6o1sjh.png" alt="emitting events for oven" width="880" height="1035"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Listening to events
&lt;/h5&gt;

&lt;p&gt;Now that our ovens are able to communicate their status with the parent component, the only thing that is left to do is to listen to their events.&lt;/p&gt;

&lt;p&gt;Navigate to &lt;code&gt;views/Kitchen1.vue&lt;/code&gt; and add the following logic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PHybpyCn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsz7b4l656jh3vvi6h6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PHybpyCn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsz7b4l656jh3vvi6h6h.png" alt="logic for Kitchen1" width="880" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;currentItems&lt;/code&gt; list is what we want to render to the screen. This can be easily achieved using the &lt;code&gt;v-for&lt;/code&gt; directive, just like we did for the &lt;code&gt;ovens&lt;/code&gt; list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---rGH18hW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92abc4hwyh3i7l39h6z9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---rGH18hW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92abc4hwyh3i7l39h6z9.png" alt="Rendering current items" width="880" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We assign &lt;code&gt;addToCurrentItems&lt;/code&gt; and &lt;code&gt;removeFromCrrenltItems&lt;/code&gt; handler to the &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;baked&lt;/code&gt; events respectively.&lt;/p&gt;

&lt;p&gt;Now if we run the app, our kitchen will have all the info it needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wgs4DA9k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jymop8sdqf2vhgm9erw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wgs4DA9k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jymop8sdqf2vhgm9erw.png" alt="Program Run" width="880" height="386"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Routing
&lt;/h1&gt;

&lt;p&gt;A single kitchen won't be able to cope with a high number of orders. So let's add two more.&lt;br&gt;&lt;br&gt;
Navigate to &lt;code&gt;src/views&lt;/code&gt; and two files &lt;code&gt;Kitchen2.vue&lt;/code&gt; and &lt;code&gt;Kitchen3.vue&lt;/code&gt;. &lt;code&gt;Kitchen2&lt;/code&gt; is just like &lt;code&gt;Kicthen1&lt;/code&gt; while &lt;code&gt;Kitchen3&lt;/code&gt; is under maintenance and is inaccessible.&lt;/p&gt;

&lt;p&gt;But how do we navigate between all these rooms? I hear you ask. Well, the answer is simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define all routes&lt;/li&gt;
&lt;li&gt;Create a router&lt;/li&gt;
&lt;li&gt;Add a navbar&lt;/li&gt;
&lt;li&gt;Assign it to the &lt;code&gt;app&lt;/code&gt; component&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Define route options
&lt;/h3&gt;

&lt;p&gt;Create a new folder &lt;code&gt;router&lt;/code&gt; inside &lt;code&gt;src&lt;/code&gt; and create an &lt;code&gt;index.js&lt;/code&gt; in &lt;code&gt;src/router&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Here, we define an array &lt;code&gt;routes&lt;/code&gt; containing the navigation options for each route (or kitchen).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y3HLHE9r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdp4a2zm9z7vj0pot96f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y3HLHE9r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdp4a2zm9z7vj0pot96f.png" alt="Define navigation routes" width="880" height="1176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a router instance
&lt;/h3&gt;

&lt;p&gt;Creating a router instance is simple:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8mOdxzi4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d63620ngbei98ernzet2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8mOdxzi4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d63620ngbei98ernzet2.png" alt="Create a router" width="880" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  What is happening?
&lt;/h5&gt;

&lt;p&gt;We define a vue router instance using the &lt;code&gt;createRouter&lt;/code&gt; macro which takes in a history mode as well as an array of route options. A route option simply needs a &lt;code&gt;path&lt;/code&gt;, an optional &lt;code&gt;name&lt;/code&gt; (this is useful for named routes. See &lt;a href="https://router.vuejs.org/guide/essentials/named-routes.html"&gt;named routes&lt;/a&gt; for more detail), and a &lt;code&gt;component&lt;/code&gt; to render, where the &lt;code&gt;component&lt;/code&gt; is a &lt;em&gt;view&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding a Navbar
&lt;/h3&gt;

&lt;p&gt;We also need a &lt;code&gt;NavBar&lt;/code&gt; to easily navigate between the kitchens.&lt;br&gt;Create a new file &lt;code&gt;NavBar.vue&lt;/code&gt; in the &lt;code&gt;components&lt;/code&gt; directory and use the &lt;code&gt;&amp;lt;router-link&amp;gt;&lt;/code&gt; tag to create navigation links.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CotFA1tC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzyu11b1sxkjgc39d3v2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CotFA1tC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzyu11b1sxkjgc39d3v2.png" alt="Create a navbar using router-link" width="880" height="836"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;router-link&amp;gt;&lt;/code&gt; acts like a &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag however, it has several benefits.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This allows Vue Router to change the URL without reloading the page, handle URL generation as well as its encoding.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using the router
&lt;/h3&gt;

&lt;p&gt;Navigate to &lt;code&gt;src/main.js&lt;/code&gt; and alter your code to match the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fHrVTEzT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn1xzhyqt49efi1clx1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fHrVTEzT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn1xzhyqt49efi1clx1t.png" alt="Assign router to app" width="880" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how does the 'App' component know where to render these components (views)? Well, we tell it of course.&lt;br&gt;Head over to &lt;code&gt;src/App.vue&lt;/code&gt; and add a &lt;code&gt;&amp;lt;router-view&amp;gt;&lt;/code&gt; tag inside the template.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FazFKKGE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56itfm0am4b5xusqirfb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FazFKKGE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56itfm0am4b5xusqirfb.png" alt="User router-view in App component" width="880" height="641"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our restaurant is looking clean and we can easily navigate between the kitchens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hcgWSXSf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dqlpud3s2lypb12vgkq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hcgWSXSf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dqlpud3s2lypb12vgkq.png" alt="Program Run" width="880" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  What about kitchen3?
&lt;/h5&gt;

&lt;p&gt;You forgot didn't you? Kitchen3 is barred! and you can not enter it.&lt;br&gt;In order to block all entrances, we need to intercept calls made to the &lt;code&gt;router&lt;/code&gt;. This can be easily done by using a &lt;a href="https://router.vuejs.org/guide/advanced/navigation-guards.html"&gt;navigation guards&lt;/a&gt; known as &lt;code&gt;router.beforeEach&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Head over to &lt;code&gt;router/index.js&lt;/code&gt;, and add the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bcXNHEiA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fgj9eieauyabtmrqjyz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bcXNHEiA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fgj9eieauyabtmrqjyz.png" alt="Block entrance for Kitchen3" width="880" height="902"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  What is happening?
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;router.beforeEach&lt;/code&gt; is called every time we make a navigation call to the &lt;code&gt;router&lt;/code&gt;. Here, we have access to &lt;code&gt;to&lt;/code&gt; and &lt;code&gt;from&lt;/code&gt; routes and we may explicitly &lt;code&gt;return false&lt;/code&gt; to cancel the navigation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZH4FT3Xl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xt8ft0wqufsfg2nlksiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZH4FT3Xl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xt8ft0wqufsfg2nlksiy.png" alt="Program Run" width="880" height="449"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Composables
&lt;/h1&gt;

&lt;p&gt;You may have noticed that we duplicated all our code logic from &lt;code&gt;Kitchen1.vue&lt;/code&gt; to &lt;code&gt;Kitchen2.vue&lt;/code&gt;, which of course, is not ideal. And that is where we use &lt;strong&gt;&lt;a href="https://vuejs.org/guide/reusability/composables.html"&gt;composables&lt;/a&gt;&lt;/strong&gt;.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;a "composable" is a function that leverages Vue's Composition API to encapsulate and reuse stateful logic.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Key points
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;They become more and more vital as the code base scales.&lt;/li&gt;
&lt;li&gt;In addition to &lt;strong&gt;reusability&lt;/strong&gt;, composables also provide &lt;strong&gt;readablity&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;I usually give each composable file the same name, in &lt;strong&gt;lowerCamelCase&lt;/strong&gt;, as the function it exports.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Extracting reusable code
&lt;/h3&gt;

&lt;p&gt;Create a new folder in the &lt;code&gt;src&lt;/code&gt; directory with the name of &lt;code&gt;composables&lt;/code&gt; and add new file &lt;code&gt;getCurrentKitchenItems&lt;/code&gt;. Define a function with the same name that encapsulates the logic for items that are currently being baked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--631K58lA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujbv0609ed3qxtbm38gr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--631K58lA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujbv0609ed3qxtbm38gr.png" alt="Extract reusable stateful logic to composable" width="880" height="692"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in both of our functioning kitchens, we can do:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3NifAvap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0entz170qznmkpfnkl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3NifAvap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0entz170qznmkpfnkl6.png" alt="Using the composable" width="880" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Much cleaner!&lt;/strong&gt; And our kitchens work smoothly as well.&lt;br&gt;&lt;br&gt;
Behind the scenes, I also added a &lt;code&gt;CoffeeMaker&lt;/code&gt; component (which works just like an &lt;code&gt;Oven&lt;/code&gt;).&lt;br&gt;You can imagine how our code would bloom up, had we not used composables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j1lQCzSW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kroad5mwyyv6irqcifry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j1lQCzSW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kroad5mwyyv6irqcifry.png" alt="Program Run" width="880" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrap up
&lt;/h1&gt;

&lt;p&gt;And with this, our little session comes to an end. You can keep adding more and components and custom logic to this project. The benefit of following &lt;strong&gt;Vue&lt;/strong&gt;'s conventions is that your code base is highly scalable.&lt;br&gt;&lt;br&gt;
Just remember to take it one step at a time, and keep finding motivation.&lt;br&gt;&lt;br&gt;
HAPPY LEARNING!!!&lt;/p&gt;

</description>
      <category>vue</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
