{"componentChunkName":"component---src-templates-blog-post-js","path":"/hello-world/","webpackCompilationHash":"23268da9a2d41a689f81","result":{"data":{"site":{"siteMetadata":{"title":"A Vue Blog","author":"Gautham Lal"}},"markdownRemark":{"id":"20dca6ac-47d0-517f-a024-9accb9ec5fd1","excerpt":"Welcome to my blog! This is a blog for absolute beginners to Vue.js, from an absolute beginner to Vue.js! But a basic understanding of HTML, CSS, and Javascript…","html":"<p>Welcome to my blog!</p>\n<p>This is a blog for absolute beginners to Vue.js, from an absolute beginner to Vue.js! But a basic understanding of HTML, CSS, and Javascript is recommended.</p>\n<p>Here I will talk about everything I cover on Vue.js and then try and teach the same to you, my faithful readers. So to kick off let’s start with the archetypal first program everyone writes. Printing <strong>Hello World!</strong> on to the screen.</p>\n<hr>\n<p>Before we get our hands dirty with code, what is Vue.js?</p>\n<p>Vue.js is a progressive Javascript framework used to build user interfaces. It’s called progressive because it is incrementally adoptable - it is easy to use and to integrate with other frameworks and libraries.</p>\n<p>I’m sure this will become more apparent to us as we go along so let’s get started.</p>\n<p>There are many possible ways to install Vue.js, but we will start easy by using the Vue.js CDN. You can use <a href=\"https://jsfiddle.net\">JSFiddle</a> or create an HTML file and attach the following script:</p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>https://cdn.jsdelivr.net/npm/vue/dist/vue.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>In the HTML file create a div tag and assign it an id ‘app’. Inside the div, write <code>{{ message }}</code>. Our HTML code will look something like this.</p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>https://cdn.jsdelivr.net/npm/vue/dist/vue.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span>\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>app<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\n  {{ message }}\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In the JS file write the following, save and then run.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> app <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Vue</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  el<span class=\"token punctuation\">:</span> <span class=\"token string\">'#app'</span><span class=\"token punctuation\">,</span>\n  data<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>\n    message<span class=\"token punctuation\">:</span> <span class=\"token string\">'Hello World!'</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Voila! And that’s our very first Vue.js app done and dusted!</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto;  max-width: 590px;\"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/cb4835abd8f165260f3ba437a097c7f2/d4ee1/hello-world.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.14678899082569%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAIBBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAe5qiVD/xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAEFAl//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAY/Al//xAAYEAADAQEAAAAAAAAAAAAAAAAAEBEBMf/aAAgBAQABPyEhFnF//9oADAMBAAIAAwAAABBDz//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABgQAQADAQAAAAAAAAAAAAAAAAEAEBEx/9oACAEBAAE/EDNYJ4Tdcq//2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Our very first app - Hello World!\"\n        title=\"Our very first app - Hello World!\"\n        src=\"/static/cb4835abd8f165260f3ba437a097c7f2/c739e/hello-world.jpg\"\n        srcset=\"/static/cb4835abd8f165260f3ba437a097c7f2/8ee9c/hello-world.jpg 148w,\n/static/cb4835abd8f165260f3ba437a097c7f2/ebbe7/hello-world.jpg 295w,\n/static/cb4835abd8f165260f3ba437a097c7f2/c739e/hello-world.jpg 590w,\n/static/cb4835abd8f165260f3ba437a097c7f2/5413e/hello-world.jpg 885w,\n/static/cb4835abd8f165260f3ba437a097c7f2/d4ee1/hello-world.jpg 1090w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n      />\n  </a>\n    </span></p>\n<p>We got the desired output, but like me, you must be wondering what just happened?</p>\n<p>As you can see, we pass an object to the <code>new Vue()</code> instance. Thanks to the CDN we attached at the beginning of the HTML file, we get access to the <code>Vue()</code> constructor. Vue.js binds itself to the element with the <code>id</code> that we provide in the <code>el</code> property. And now everything, from what to display in the element to managing how it behaves during events can be controlled from the Vue instance.</p>\n<p>Now you must have noticed this <code>data</code> property. All the properties inside it are used to store the values needed in our app. These properties are now added to Vue’s reactivity system. Whenever the values of these properties change, the view will rerender and the new value will be shown. Go to the console of your browser and set <code>app.message</code> to a new value and you can see the output change.</p>\n<p>Vue.js uses template syntax - the curly brackets - to bind the DOM to the Vue instance’s <code>data</code>. In <code>{{message}}</code>, the tag gets replaced with the value of <code>message</code> property. Now whenever the value of this property changes the tag will get updated.</p>\n<p>On a side note, the properties defined in <code>data</code> are only reactive if they existed when the instance was created. So if you go ahead and create a new property called <code>app.newMessage = ‘World says hello back!’</code>, any changes to <code>newMessage</code> will not cause a rerender.</p>\n<p>So there you have it. Your very first Vue app where you say hello to the world! As a “homework/exercise” you could display your name and a title you want to give yourself in separate tags. Give yourself a cool title. Too bad ‘Star-Lord’ is taken.</p>\n<p>If you have any corrections, suggestions or feedback for me, feel free to reach out to me at <a href=\"https://twitter.com/MightyGL\">twitter</a>.</p>\n<p>See you next time when we jump into <strong>directives</strong>.</p>","frontmatter":{"title":"Hello World!","date":"August 07, 2019","description":"Let's start with the archetypal first program - Hello World!"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/hello-world/","previous":null,"next":{"fields":{"slug":"/fantastic-directives-and-how-to-use-them/"},"frontmatter":{"title":"Fantastic Directives and How To Use Them!"}}}}}