A Vue application is a combination of various components, bound together and orchestrated by the Vue framework. Knowing how to make your component is important. Each component is like a brick in the wall and needs to be made in a way that, when placed, doesn't end up needing other bricks to be reshaped in different ways around it. We are going to learn how to make a base component, with some important principles that focus on organization and clean code.
Getting ready
The pre-requisite for this recipe is as follows:
Node.js 12+
The Node.js global objects that are required are as follows:
@vue/cli
@vue/cli-service-global
How to do it...
To start our component, we can create our Vue project with Vue CLI as learned in the 'Creating your first project with Vue CLI' recipe in Chapter 2,Introducing TypeScript and the Vue Ecosystem, or start a new one.
To start a new one, open Terminal (macOS or Linux) or Command Prompt/PowerShell (Windows) and execute the following command:
> vue create my-component
The command-line interface (CLI) will ask some questions that will help with the creation of the project. You can use the arrow keys to navigate, the Enter key to continue, and the spacebar to select an option. Choose the defaultoption:
? Please pick a preset:(Use arrow keys) ? default (babel, eslint) Manually select features ?
Let's create our first "hello world" component, following these steps:
Let's create a new file called CurrentTime.vue file in the src/components folder.
On this file, we will start with the <template> part of our component. It will be a shadowed-box card that will display the current date formatted:
Now, we need to create the <script> part. We will start with the name property. This will be used when debugging our application with vue-devtools to identify our component and helps the integrated development environment (IDE) too. For the getCurrentDate computed property, we will create a computed property that will return the current date, formatted by the Intl browser function:
In the main.js file, we need to import the style.css file to be included in the Vue application:
import Vue from 'vue'; import App from './App.vue'; import './style.css';
Vue.config.productionTip = false
new Vue({ render: h => h(App), }).$mount('#app')
To run the server and see your component, you need to open Terminal (macOS or Linux) or Command Prompt/PowerShell (Windows) and execute the following command:
> npm run serve
Here is your component rendered and running:
How it works...
The Vue component works almost like the Node.js packages. To use it in your code, you need to import the component and then declare it inside the components property on the component you want to use.
Like a wall of bricks, a Vue application is made of components that call and use other components.
For our component, we used the Intl.DateTimeFormat function, a native function, which can be used to format and parse dates to declared locations. To get the local format, we used the navigator global variable.