Learning a new framework can be a very daunting process for any developer, especially for one that is still learning the base language (in this case JavaScript). This is why I have decided to create this series in which I will attempt to make the learning of Vue.js as easy and digestible as possible 🙂
I'm not a fan of making long drawn out introductions, so I will assume that if you're still reading:
You have some basic HTML/CSS/JS knowledge. You don't need to be an experienced front-end developer to take on on Vue as a development framework, but at the very least you need to be able to write your own HTML markup, understand the basic of how CSS works and, yes, how to write javascript. In the end, this is what this is all about.
That's it. No, really.
Vue as a library
There are several ways in which you can incorporate Vue into your web project. Let's start with the simplest one (which you will probably not end up using a lot).
Most tutorials/articles will assume that you have some understanding of how to set up a development environment in which you will use things like npm
, webpack
to set up your project - and while this is ideal because of what you get out of the box - we can start with a much simpler beginner-friendly approach. The reliable old <script>
tag.
Go ahead and fire up your favorite code editor, and create a new file called index.html
. (If you don't have one yet, VS Code is a popular free choice.
<html>
<head>
<title>Vue 101</title>
</head>
<body>
<h1>Hello!</h1>
<div id="app"></div>
</body>
</html>
Nothing fancy, we're just setting the bones for a simple website. Now let's get the Vue
library in there. Paste this script tag before your closing </body>
.
[...]
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</body>
Now that Vue
is being loaded into our page, we can start using it.
Let's go ahead and create a new Vue instance, by new
ing it up inside a <script>
tag. We will give it a selector by passing #app
to the el
property of the options object, and that way Vue
will know where our app should be rendered. (Remember that empty <div>
with an ID of app?)
Place this code after our last script tag.
<script>
const app = new Vue({
el: '#app', // 1
data: { // 2
myLocalProperty: 'Im a local property value' // 3
}
});
</script>
So what's happening here?
We created our new Vue
instance, and pass it a configuration object. See the {}
as a parameter?
-
el:
As I mentioned before, here we tell Vue where inside our HTML we want our app to be displayed. In this case, the div with theapp
id. -
data
object. Every Vue instance has a local storage, like a box of variables and properties that it will hold for us and that we can use when coding our app. Data holds a JavaScriptobject
, so we assign it one with the{ }
syntax. Inside, we place a property. -
myLocalProperty
. This property is defined inside thedata
object for our instance, it's name is myLocalProperty and the value on the right-hand side is the value - in this case, a string.
Displaying properties on our app
Right now if you open up index.html
in your browser, not much is happening.
Let's add some code to display our property inside the HTML. Your file should look like this:
<html>
<head>
<title>Vue 101</title>
</head>
<body>
<h1>Hello!</h1>
<div id="app">
<p>My local property: {{ myLocalProperty }}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
myLocalProperty: 'Im a local property value'
}
});
</script>
</body>
</html>
Pay close attention to this line:
<p>My local property: {{ myLocalProperty }}</p>
What's happening here is called variable interpolation, which is a fancy term for "I'm going to display the content of my myLocalProperty
variable in this placeholder where my {{ }}
are now.
Reload the page, and you will now see the string updates to reflect our variable.
Go ahead and try to change the string inside myLocalProperty
to some other text and reload the page, you should see the text update accordingly.
Reactivity
Finally, for this lesson, let's talk about reactivity
. You may have heard that Vue is a reactive framework. But what exactly does this mean? Open up your console in the chrome developer tools, and with your index.html loaded type:
app.myLocalProperty = 'Vue is reactive';
You will see the page react to this variable change!
Stay tuned for part two!
Top comments (29)
I don't think commenting on profile pics, either complimentary or not, is helpful. At all. Please refrain.
Great intro! Vue seems like a great first framework to learn.
Thanks Andy! Stay tuned for next part, will look at responding to events and methods :)
Are you serious? This is totally inappropriate. Women can code no matter how they look.
Great, finally a framework tutorial without all that node/npm/webpack mess. Thanks a lot!
Oh, we'll get to that in due time, but not nose first. It can be very daunting! :D
Lovely intro..i'm waiting for the part 2. But i love the cli approach.But this is great for beginners.. Thumbs up!!
Hey Jonathan, thanks. Yeah CLI is a great tool, and it will show up sometime in the series but it's a big overwhelming for beginners. Keep tuned for more :)
great..i will be looking forward to it.
True Marina, i'am new to vue and this beginner's guide actually helps. thanks,
Thanks for the tutorial. Distilling a topic to its essence is a great way to learn a subject. Even understanding what is happening once it is deployed to production is important: KISS. I'm still learning Vue but part of the strength of Vue seems to be its simplicity, i.e. do one thing and do it well.
Hi Marina. Recently I have started learning vue.js. So I am a beginner on vue and I am building a multistep form wizard in vue and I i am following your beginner guide but I was stuck at one approach. Like displaying a form based on category selection. So here we have a category field in html and whatever user enters in the category based on that enter category I want to prompt the next step. To achieve this i need some help
Hi Marina! Nice tutorial on Vue, really appreciate it. However, I can't seem to make the first part works. I followed exactly the code above, but myLocalProperty value was not shown! Is there something that I'm missing?
Very easy to follow! I am totally new to web development. Thanks a lot..:)
Glad you're enjoying the series Pravin. Thanks for reading!
Hey Marina, can't wait for you to do some single page apps with Vue js😉