Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
Sample Chrome application using vue.js in the list of chrome apps
Congratulations! You have just created a Chrome application!
NPM
NPM installation method is recommended for large-scale applications. Just run npm install vue:
# latest stable release
npm install vue
# latest stable CSP-compliant release
npm install vue@csp
And then require it:
var Vue = require(“vue”);
Or, for ES2015 lovers:
import Vue from “vue”;
Our HTML in our example will look exactly like in the previous examples:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue.js - NPM Installation</title>
</head>
<body>
<div id="app">
<h1>{{ message }}</h1>
</div>
<script src="main.js"></script>
</body>
</html>
Now let’s create a script.js file that will look almost exactly the same as in standalone or CDN version with only difference that it will require vue.js:
var Vue = require("vue");
var data = {
message: "Learning Vue.js"
};
new Vue({
el: "#app",
data: data
});
Let’s install vue and browserify in order to be able to compile our script.js into the main.js file:
npm install vue –-save-dev
npm install browserify –-save-dev
In the package.json file add also a script for build that will execute browserify on script.js transpiling it into main.js. So our package.json file will look like this:
{
"name": "learningVue",
"scripts": {
"build": "browserify script.js -o main.js"
},
"version": "0.0.1",
"devDependencies": {
"browserify": "^13.0.1",
"vue": "^1.0.25"
}
}
Now run:
npm install
npm run build
And open index.html in the browser.
I have a friend that at this point would say something like: really? So many steps, installations, commands, explanations… Just to output some header? I’m out!
If you are also thinking this, wait. Yes, this is true, now we’ve done something really simple in a rather complex way, but if you stay with me a bit longer, you will see how complex things become easy to implement if we use the proper tools. Also, do not forget to check your Pomodoro timer, maybe it’s time to take a rest!
Vue-cli
Vue provides its own command line interface that allows bootstrapping single page applications using whatever workflows you want. It immediately provides hot reloading and structure for test driven environment. After installing vue-cli just run vue init <desired boilerplate> <project-name> and then just install and run!
# install vue-cli
$ npm install -g vue-cli
# create a new project
$ vue init webpack learn-vue
# install and run
$ cd learn-vue
$ npm install
$ npm run dev
Now open your browser on localhost:8080. You just used vue-cli to scaffold your application. Let’s adapt it to our example. Open a source folder. In the src folder you will find an App.vue file. Do you remember we talked about Vue components that are like bricks from which you build your application? Do you remember that we were creating them and registering inside our main script file and I mentioned that we will learn to build components in more elegant way? Congratulations, you are looking at the component built in a fancy way!
Find the line that says import Hello from './components/Hello'. This is exactly how the components are being reused inside other components. Have a look at the template at the top of the component file. At some point it contains the tag <hello></hello>. This is exactly where in our HTML file will appear the Hello component. Have a look at this component, it is in the src/components folder. As you can see, it contains a template with {{ msg }} and a script that exports data with defined msg. This is exactly the same what we were doing in our previous examples without using components. Let’s slightly modify the code to make it the same as in the previous examples. In the Hello.vue file change the msg in data object:
<script>
export default {
data () {
return {
msg: “Learning Vue.js”
}
}
}
</script>
In the App.vue component remove everything from the template except the hello tag, so the template looks like this:
<template>
<div id="app">
<hello></hello>
</div>
</template>
Now if you rerun the application you will see our example with beautiful styles we didn’t touch:
vue application bootstrapped using vue-cli
Besides webpack boilerplate template you can use the following configurations with your vue-cli:
webpack-simple: A simple Webpack + vue-loader setup for quick prototyping.
browserify: A full-featured Browserify + vueify setup with hot-reload, linting and unit testing.
browserify-simple: A simple Browserify + vueify setup for quick prototyping.
simple: The simplest possible Vue setup in a single HTML file
Dev build
My dear reader, I can see your shining eyes and I can read your mind. Now that you know how to install and use Vue.js and how does it work, you definitely want to put your hands deeply into the core code and contribute!
I understand you. For this you need to use development version of Vue.js which you have to download from GitHub and compile yourself.
Let’s build our example with this development version vue. Create a new folder, for example, dev-build and copy all the files from the npm example to this folder.
Do not forget to copy the node_modules folder. You should cd into it and download files from GitHub to it, then run npm install and npm run build.
cd <APP-PATH>/node_modules
git clone https://github.com/vuejs/vue.git
cd vue
npm install
npm run build
Now build our example application:
cd <APP-PATH>
npm install
npm run build
Open index.html in the browser, you will see the usual Learning Vue.js header.
Let’s now try to change something in vue.js source! Go to the node_modules/vue/src folder. Open config.js file. The second line defines delimeters:
let delimiters = ['{{', '}}']
This defines the delimiters used in the html templates. The things inside these delimiters are recognized as a Vue data or as a JavaScript code. Let’s change them! Let’s replace “{{” and “}}” with double percentage signs! Go on and edit the file:
let delimiters = ['%%', '%%']
Now rebuild both Vue source and our application and refresh the browser. What do you see?
After changing Vue source and replacing delimiters, {{}} delimeters do not work anymore!
The message inside {{}} is no longer recognized as data that we passed to Vue. In fact, it is being rendered as part of HTML.
Now go to the index.html file and replace our curly brackets delimiters with double percentage:
<div id="app">
<h1>%% message %%</h1>
</div>
Rebuild our application and refresh the browser! What about now? You see how easy it is to change the framework’s code and to try out your changes. I’m sure you have plenty of ideas about how to improve or add some functionality to Vue.js. So change it, rebuild, test, deploy! Happy pull requests!
Debug Vue application
You can debug your Vue application the same way you debug any other web application. Use your developer tools, breakpoints, debugger statements, and so on. Vue also provides vue devtools so it gets easier to debug Vue applications. You can download and install it from the Chrome web store: https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd
After installing it, open, for example, our shopping list application. Open developer tools. You will see the Vue tab has automatically appeared:
Vue devtools
In our case we only have one component—Root. As you can imagine, once we start working with components and having lots of them, they will all appear in the left part of the Vue devtools palette. Click on the Root component and inspect it. You’ll see all the data attached to this component. If you try to change something, for example, add a shopping list item, check or uncheck a checkbox, change the title, and so on, all these changes will be immediately propagated to the data in the Vue devtools. You will immediately see the changes on the right side of it. Let’s try, for example, to add shopping list item. Once you start typing, you see on the right how newItem changes accordingly:
The changes in the models are immediately propagated to the Vue devtools data
When we start adding more components and introduce complexity to our Vue applications, the debugging will certainly become more fun!
Summary
In this article we have analyzed the behind the scenes of Vue.js. We learned how to install Vue.js. We also learned how to debug Vue application.
Resources for Article:
Further resources on this subject: