Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Vue.js 2.x by Example

You're reading from   Vue.js 2.x by Example Example-driven guide to build web apps with Vue.js for beginners

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781788293464
Length 412 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mike Street Mike Street
Author Profile Icon Mike Street
Mike Street
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with Vue.js 2. Displaying, Looping, Searching, and Filtering Data FREE CHAPTER 3. Optimizing your App and Using Components to Display Data 4. Getting a List of Files Using the Dropbox API 5. Navigating through the File Tree and Loading Folders from the URL 6. Caching the Current Folder Structure Using Vuex 7. Pre-Caching Other Folders and Files for Faster Navigation 8. Introducing Vue-Router and Loading URL-Based Components 9. Using Vue-Router Dynamic Routes to Load Data 10. Building an E-Commerce Store – Browsing Products 11. Building an E-Commerce Store – Adding a Checkout 12. Using Vue Dev Tools and Testing Your SPA

Creating the workspace

To use Vue, we first need to include the library in our HTML and initialize it. For the examples in the first section of this book, we are going to be building our application in a single HTML page. This means the JavaScript to initialize and control Vue will be placed at the bottom of our page. This will keep all our code contained, and means it will easily run on your computer. Open your favorite text editor and create a new HTML page. Use the following template as a starting point:

      <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue.js App</title>
</head>
<body>
<div id="app">
</div>
<script src="https://unpkg.com/vue"></script>
<script type="text/javascript">
// JS Code here
</script>
</body>
</html>

The main HTML tags and structure should be familiar to you. Let's run over a few of the other aspects.

Application space

This is the container for your application and provides a canvas for Vue to work in. All the Vue code will be placed within this tag. The actual tag can be any HTML element - main, section, and so on. The ID of the element needs to be unique, but again, can be anything you wish. This allows you to have multiple Vue instances on one page or identify which Vue instance relates to which Vue code:

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

During the tutorials, this element with the ID will be referred to as the app space or view. It should be noted that all HTML and tags and code for your application should be placed within this container.

Although you can use most HTML tags for your application space, you cannot initialize Vue on the <body> or <HTML> tags - if you do so, Vue will throw a JavaScript error and will fail to initialize. You will have to use an element inside your body.

Vue library

For the examples in this book, we are going to be using a hosted version of Vue.js from a CDN (Content Delivery Network) unpkg. This ensures that we have the latest version of Vue in our application, and also means we do not need to create and host other JavaScript files. Unpkg is an independent site that hosts popular libraries. It enables you to quickly and easily add a JavaScript package to your HTML, without having to download and host the file yourself:

      <script src="https://unpkg.com/vue"></script>

When deploying your code, it is a good practice to serve up your libraries from local files rather than relying on CDNs. This ensures that your implementation will work with the currently - saved version, should they release an update. It will also increase the speed of your application, as it will not need to request the file from another server.

The script block following the library include is where we are going to be writing all our JavaScript for our Vue application.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime