Vue is a progressive JavaScript framework that focuses on building user interfaces. It focuses heavily on the ViewModel. To learn more, go over to the documentation to see things for yourself.
In this tutorial, you will get your hands dirty with Vue.js by building a simple blog. The blog will have a form that allows you create a post. However, the post that will be displayed on the blog will be retrieved from a resource available online.
Sounds like fun? Let us get busy.
Getting Started
Start by running the command:
vue init webpack blogapp
Now migrate into the newly created folder and run the command to install your modules.
npm install
This will install all the modules listed in your package.json.
Now start up your dev server to see what you have. Simply run the command below.
npm run dev
Input Binding
First, create a new file called addBlog.vue in your src/components folder. This file you are creating is called a component.
Components are used to extend basic HTML elements to encapsulate reusable code. This component will handle the creation of new blog post. Here is how the file should look.
#src/components/addBlog.vue
Add a New Blog Post
Thanks for adding your post
Preview blog
Blog title: {{ blog.title }}
Blog content:
{{ blog.content }}
Blog Categories:
{{ category }}
Author: {{ blog.author }}
In the template section, you have two parts. The first is a form to create a new post, and the other gives a preview of what is entered in the form. With this, you can see the inputs you enter before saving the post. This is possible using a v-model, which is a directive that allows us to perform two-way data bindings on form inputs. It automatically updates the DOM based on the input entered. The data binding is also possible for select options and checkboxes, as you can see above.
A directive is a special token in the markup that tells the library to do something to a DOM element.
For instance, the line of code will bind the value entered into the field to be the value of blog.title. This value according to what you have above is outputted where you have
Blog title: {{ blog.title }}
.
v-for is used to loop through a collection. In the above, you use v-for to loop through a collection of authors and categories.
It is possible to make use of conditionals in Vue. You see that play out when you set the default value of submitted to false. The line
This website uses cookies to improve your experience. AcceptRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.