Welcome to CodeCrew Infotech

shape shape
Shape Shape Shape Shape
Blog

Introduction to Vue.js for beginners

What is Vue.js?

Vue.js, commonly referred to as Vue, is a progressive JavaScript framework used for building user interfaces (UIs) and single-page applications (SPAs). Vue is designed to be incrementally adoptable, meaning that developers can integrate it into existing projects or use it to build new applications, gradually incorporating its features as needed.

 

Key features of Vue.js applications include:

 

1. Easy-to-Understand Design: Vue.js makes it simple for developers to create and manage the visual part of a website or app. You tell Vue.js what you want, and it takes care of the technical details to make it happen.
 
2. Building Blocks (Components): It lets developers create building blocks called "components." These are like LEGO pieces that you can reuse to build different parts of a website or app. This helps save time and keeps things organized.
 
3. Automatic Updates: When the information (data) behind your website or app changes, Vue.js automatically updates what users see on their screens. This helps in keeping everything up-to-date without extra work for the developer.
 
4. Special Commands (Directives): Vue.js has special commands called "directives" that you can use to tell it how to handle different parts of your website. For example, you can use a directive to show or hide something based on certain conditions.
 
5. Easy-to-Write Templates: You write the structure of your website or app using a simple and clear template. Vue.js then takes care of turning that into the actual website that users interact with.
 
6. Navigation Support (Vue Router): If your website has different pages, Vue.js has a tool called Vue Router to help with moving between these pages smoothly. It's like having a map for your website.
 
7. Managing Information (Vuex): When your website or app has a lot of information that needs to be shared between different parts, Vue.js has Vuex to help manage that iformation in a smart way. It's like a central hub for all the important stuff.
 
8. Ready-to-Use Tools (Vue CLI): For developers who want a head start, Vue.js provides a tool called Vue CLI. It sets up everything needed for a project quickly, making it easier to get started with building something awesome.
 
9. Works Well with Others: Vue.js plays nicely with other tools and libraries. This means developers can use it alongside their favorite tools without any trouble.
 
10. Helpful Community: Vue.js has a friendly community of developers who are always ready to help and share cool ideas. This means you're not alone – there's a whole bunch of people building things with Vue.js!
 

How to initialize a Vue.js project?

 

1. Install Node.js:
Visit https://nodejs.org/ in your web browser.
Download the recommended "LTS" version for your operating system (Windows, macOS, or Linux).
Run the installer and follow the on-screen instructions to complete the Node.js installation.

 

2. Install Vue CLI (Command-Line Interface):
Open your terminal or command prompt.
Run npm install -g @vue/cli to install Vue CLI globally on your system
 
3. Create a Vue.js Project:
Navigate to the directory where you want to create your Vue.js project using the terminal or command prompt.
Execute vue create my-vue-project to create a new Vue.js project
Replace "my-vue-project" with your preferred project name.

 

4. Run Your Vue.js Application:
run a Vue.js project using the npm run serve command, the default port number 8080 assigned to the Vue

 

Life cycle of Vue.js:

Vue.js components have a lifecycle that consists of various phases, allowing developers to execute code at specific points in the component's existence. The lifecycle hooks are categorized into three main phases: Creation, Mounting, and Updating/Destruction

 

Creation Lifecycle Hooks:

1. BeforeCreate: Triggered before the instance is created. Data observation and event initialization have not occurred yet.
2. Created: Called synchronously after the instance has been created. At this stage, the component has initialized its data and events.

 

Mounting Lifecycle Hooks:

1. BeforeMount: Triggered before the component is mounted to the DOM. The virtual DOM has been created, but it hasn't been rendered yet.

2. Mounted: Called after the component has been mounted to the DOM. This is a good place to perform initial DOM manipulations, set up watchers, or make API requests.

 

Updating Lifecycle Hooks:

1. BeforeUpdate: Triggered before a component re-renders due to changes in data. At this point, the virtual DOM is updated but not yet patched to the actual DOM.

2. Updated: Called after a component has been re-rendered and the changes have been patched to the DOM. It's often used when you need to perform actions after a component has been updated due to data changes.

 

Destruction Lifecycle Hooks:

1. BeforeDestroy: Triggered just before a component is destroyed. It's an opportunity to clean up resources, such as removing event listeners or clearing timers.

2. Destroyed: Called after a component has been destroyed. At this point, all directives are unbound, and all event listeners are removed.

 

Additional Hooks:

1. Activated: Called when a kept-alive component is activated (switched to).

2. Deactivated: Called when a kept-alive component is deactivated (switched away).

 

What is a Watcher in Vue.js?

A "watcher" refers to a feature that allows you to perform some logic or actions when a specific data property changes. Watchers are used to observe changes in data and react accordingly

For Example:

 watch: {

    // This function will be called whenever 'inputText' changes

    inputText: function(newInput, oldInput) {

      this.message = 'You typed: ' + newInput;

    }

  }

 

watch: This is an option in Vue.js that allows you to define watchers for specific data properties.
inputText: This specifies the property you want to watch. In this case, it's the inputText property.
function(newInput, oldInput): This is the function that will be called when the inputText property changes. It takes two parameters: newInput (the new value of inputText) and oldInput (the previous value of inputText).

 

What is computed in Vue.js?

The computed option is used to define properties that are dependent on other reactive data properties. Computed properties are like data properties, but they are derived from other data properties and are automatically updated when those properties change.

For Example:

computed: {

    // This computed property depends on the 'radius' data property

    area: function() {

      // Calculate the area based on the radius

      return Math.PI * Math.pow(this.radius, 2);

    }

  }

 
computed: This is an option in Vue.js used to define computed properties. Computed properties are properties that are derived from other reactive data properties.
area: This is the name of the computed property. You can use this name in your Vue template to display or use the computed value.
function() { ... }: This is the function associated with the computed property. It is the logic that calculates the value of the computed property.