10 Minute Vue App
Image credit: Bru-nO via Pixabay (Vue-ified by scoffin)

10 Minute Vue App

The Intro

Vue is a front-end JavaScript framework that packs a lot of the same features of other popular frameworks (e.g., Angular and React) into a slimmer, easier-to-learn package. To prove it, I'm going to show you how to get a very simple Vue app up and running in just 10 minutes*! If you want to just skip to the code, grab the repo from Github.

* Common side effects include a heightened respect for JavaScript, bragging about your new skills, and curiosity about what more Vue has to offer. Please consult the Vue.js documentation if you experience any of these symptoms. Some restrictions apply. Individual results may vary.


The Prerequisites

  • Text editor/IDE (e.g.,Visual Studio Code)
  • Internet connection
  • Browser
  • Solid understanding of HTML
  • Fair understanding of JavaScript


The App Structure

One folder and two files is all we're going to need here.

(1) Create a folder called ten-minute-vue-app.

(2) In that folder, create two files: index.html and app.js.


The Code

The HTML page (index.html) is going to contain an element that is mounted with a Vue instance, which will be defined in our Javascript file (app.js) later. Any data and methods that you define in this Vue instance will be accessible within this element, in addition to Vue's native functionality.


(3) Add your basic HTML boilerplate code to index.html.

<!DOCTYPE?html>

<html>

????<head>
????????<title>10?Minute?Vue?App</title>
????</head>

????<body>
        <h1>10 Minute Vue App</h1>
??? </body>
</html>
        


To inject Vue into your HTML, all you need to do is reference a link to it in the CDN (content delivery network) with a script tag.

(4) In the <head> of your file, add Vue to your HTML.

<script?src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        


Since your Vue instance will live in app.js, you'll need to connect this to your HTML, too.

(5) Immediately above the closing <body> tag, add app.js to your HTML.

<script?src="app.js"></script>
        


As we mentioned earlier, the HTML file is going to contain an element that will be mounted with a Vue instance. This element is nothing special yet, but it will be soon.

(6) In the <body> element, above your <script> tag for app.js, create a div to mount the Vue instance to. Give it an id of "app."

<div id="app">
</div>
        


At this point, your HTML file should look something like this:

No alt text provided for this image


Now that we've hooked-up our HTML, let's jump into that JS file to create our Vue instance.

(7) Create a Vue instance in app.js.

new?Vue({

???
});
        


This Vue instance is currently useless. It contains nothing and is mounted to nothing. Let's change that.

(8) Mount the Vue instance to the div living in our HTML by specifying that element as the value for the el property ("el" is short for "element.") We'll identify it by using the CSS selector for its id.

new?Vue({
    el: "#app"
});
        


Now that the connection between the HTML element (#app) and the Vue instance has been established, we can start sharing data and methods between them.

(9) Define a value in the Vue instance's data property. The data property is an object containing various key:value pairs of data.

new?Vue({
????el:?"#app",
????data:?{
????????"confirmation":?"Your?Vue?data?is?working!"
    }
});

        


(10) Access that value in the HTML using mustache syntax (a.k.a. double curly braces).

<div?id="app">
    <p>{{ confirmation }}</p>
</div>
        


At this point, if you haven't already done so, open your index.html file in your browser of choice to see your HTML and JS playing together nicely.

HTML page displaying data from the Vue instance


Now that we've seen Vue pass data for us, let's whip-up a method and call it from our HTML.

(11) Define a method in the Vue instance's method property. Like the data property, the method property is also an object.

new?Vue({
????el:?"#app",
????data:?{
????????"confirmation":?"Your?Vue?data?is?working!"
????},
????methods:?{
????????sayHello()?{
????????????alert(`Hello!`)
????????}
    
    }
});
        


(12) Use this new method in the HTML by creating a button element that listens for a click event and triggers the method call as a response*.

* The v-on syntax is part of a Vue directive, which is beyond the scope of this article. Please see Intro to Vue Directives (coming soon) if you want to learn more.

<div?id="app">
????<p>{{?confirmation?}}</p>
    <button?v-on:click="sayHello()">Greet?Me</button>
????????
</div>
        


Now when you look at this in the browser, you should see the following alert when you click on the "Greet Me" button. If you didn't code along, you can also see this in action by checking out the CodePen preview.

JavaScript alert triggered by clicking on the "Greet Me"? button


Conclusion

With this simple application, you've just unlocked the door to using Vue.js. Part of the beauty of Vue is its low barrier to entry, but don't let this fool you into thinking it's a shallow framework. This article was just a taste of what Vue has to offer, so we didn't even touch on other amazing features like directives and components, so there's tons more to explore!

要查看或添加评论,请登录

Sondra Coffin的更多文章

  • Welcome to the Wonderful World of HTML

    Welcome to the Wonderful World of HTML

    HTML is a computing language that is used to “tell” computers how a web page should be structured. It is a fundamental…

  • Use the Visual Studio Code "Prophet Debugger" extension to connect to a Salesforce Commerce Cloud sandbox

    Use the Visual Studio Code "Prophet Debugger" extension to connect to a Salesforce Commerce Cloud sandbox

    I just finished working my way through a Medium article by Manu Saini regarding connecting VSCode (Visual Studio Code)…

    1 条评论
  • Pug.js Basics

    Pug.js Basics

    Pug (formerly known as Jade) is a template engine that allows you to dynamically manipulate the HTML and CSS that are…

    6 条评论
  • Getting Started with Asynchronous JavaScript

    Getting Started with Asynchronous JavaScript

    When I'm trying to learn something new and am finding trouble wrapping my head around the overarching concept, my first…

  • Git Refresher

    Git Refresher

    * * * PLEASE NOTE * * * ?As of June 2020, GitHub now refers to the "master" branch as the "main" branch by default…

  • Quick Tip: Shortcut a query in Oracle SQL Developer or SQL Server Management Studio

    Quick Tip: Shortcut a query in Oracle SQL Developer or SQL Server Management Studio

    After going back and forth copy-and-pasting or trying to remember queries for my current project, I decided wanted to…

  • My Journey into SOAP

    My Journey into SOAP

    Introduction This article embodies my understanding of SOAP-- which is admittedly simplified in some areas-- in the…

    2 条评论
  • 3 Quick Tips for Absolute Beginner Coders

    3 Quick Tips for Absolute Beginner Coders

    I've gotten a lot of messages from people across the world asking about my journey into code, so I wanted to take a few…

    2 条评论
  • Why You Should Hire a Candidate with Teaching Experience

    Why You Should Hire a Candidate with Teaching Experience

    Having been an educator for several years, I know firsthand how valuable teachers' knowledge, skills, and work ethic…

    6 条评论
  • A Teacher's To-Do List

    A Teacher's To-Do List

    I recently wrote an article for staffing managers on Why You Should Hire a Candidate with Teaching Experience, and…

    2 条评论

社区洞察

其他会员也浏览了