Vue

How to Create Components in Vue CLI

How to Create Components in Vue CLI
Vue.js provides the Vue CLI to provide the vue command inside the terminal for quickly scaffolding a new project of Vue.js and run the Vue.js project using the vue serve command. Vue.js also provides the graphical user interface for managing the projects using the vue ui command. Vue.js is recognized as combining two spectacular frameworks, Angular and React, using the templating syntax of Angular and props method of React. It provides the traditional HTML and CSS way to create a component, and in this post, we will go through the process of creating and understanding components in Vue CLI.

Prerequisites

Before getting started with this, there are some prerequisites that you must have:

Verify the Vue CLI installation

First of all, make sure that you have the latest Vue CLI installed on your system. You can verify either Vue CLI is installed or not on our system by typing the command given below:

$ vue --version

If it is installed, you will have the latest version of Vue CLI printed out in the terminal. Otherwise, if it is not installed, you can either use the NPM package manager or Yarn package manager to install the Vue CLI. In order to install it using the NPM package manager, you need to type the command given below in the terminal:

$ npm install -g @vue/cli

In the above command, the -g flag is used for installing Vue CLI globally on your system.

Once the Vue CLI is completely installed, you can verify it by typing the command given below:

$ vue --version

You will have the latest version of Vue CLI in the output.

Project Creation

Now, suppose you are going to set up the whole Vue project on your own. In that case, it is not a good choice to reinvent the wheel; the Vue project can be created using the vue command in the terminal because the Vue CLI provides the already generated templates to start with the Vue project.

To create the Vue application, simply type the command given below in the terminal:

$ vue create project-name

Make sure to replace the project-name with your desired project name and hit Enter.

After a couple of seconds, it will prompt selecting the default preset or selecting some features manually.

If you want to have some custom features, select “Manually select features,” hit Enter, and you will be prompted with some options like selecting the Vue version, adding Vuex, or Router. Select your desired option and hit Enter.

Answer some necessary configuration questions and save the preset for future projects.

The Vue project will be created in a while using the Vue CLI, and you can start the development in Vue.js.

Starting the Vue application

Once the Vue project is created, you can start the project by first navigating to the project's directory using the cd command in the terminal:

$ cd project-name

In the project's directory, start the Vue application by typing the command given below in the terminal:

$ npm run serve

After the ignition of the Vue application, visit the http://localhost:8080 in the address bar of your favorite browser:

You will have the Welcome screen of the Vue.js project.

Creation of a Component in Vue

For creating a component in the Vue project, create a .vue file in the components folder and provide it the name of your choice.

Now, in this newly created .vue file, you can write HTML, Javascript, and CSS in the