Web Analytics
  • Culture
      Back
      Agile Mindset

      Agile is not a principal or a method, but it’s an integral part of being Agile that is guided by principles, defined by values and manifested through various practices.

      Bacancy Values

      You add value to your customer when you deliver a product or service that has been designed specifically to solve their problem.

      Bacancy Culture

      Core Team will work as Scrum Team where Team will have quarterly goal to make sure that we run financial, administrative and project management prospective.

  • What we do
      Back
      Product Engineering

      Seize the opportunity to make your product stand out. We enable our clients

      AI & ML

      We automate businesses and optimize processes by deploying smart AI and...

      Blockchain

      Get a full spectrum of blockchain development services from us to bring scalability...

      IOT

      Improve Business Productivity and Efficiency using our high-end IOT professional services...

      Digital Transformation

      We truly become a part of your business by helping you think through the...

  • Who we work with
      Back
      Real Estate

      We can help you uncover the hidden revenue opportunities to showcase your...

      Finance & Insurance

      In the emerging technological environment, we are offering reliable banking and financial...

      Oil & Gas

      Reshape your energy landscape and gain better control by harnessing the best...

      Healthcare

      Utilizing advanced technologies to provide best software, web & mobile development services...

      Travel & Transport

      Manage your logistics and transportation business at the ease of your fingertips...

      Startups

      We can help you to build your MVP with advanced technologies....

  • About Us
      Back
      About

      Agile, A Process Delivering Values & Successful Products

      Blog

      Abstract Technology News Driven by Sources

      Career

      If you are passionate about your career, have creative flair and good craft skills, we want you!

  • Technology
      Back

      Front-End

      AngularJS ReactJS Vue.JS JavaScript Backbone.JS Ember.JS MEAN MERN

      Back-End

      Ruby on Rails Node.JS Golang Laravel PHP Python .NET Yii

      Mobile

      Android iOS React Native Flutter Ionic Kotlin

      CMS & CRM

      Spree Magento Wordpress Drupal Umbraco Woocommerce Salesforce Microsoft Dynamics 365<
      Explore All
  • Talk to Us
Talk to Us
Close
    MENU
  • Culture
    • Agile Mindset
    • Bacancy Values
    • Bacancy Culture
  • What we do
    • Product Engineering
    • AI & ML
    • Blockchain
    • IOT
    • Digital Transformation
  • Who we work with
    • Real Estate
    • Finance & Insurance
    • Oil & Gas
    • Healthcare
    • Travel & Transport
    • Startups
  • About Us
    • About
    • Blog
    • Career
  • Technology
      Front-End
    • AngularJS
    • ReactJS
    • Vue.JS
    • JavaScript
    • Backbone.JS
    • Ember.JS
    • MEAN
    • MERN
    • Back-End
    • Ruby on Rails
    • Node.JS
    • Golang
    • Laravel
    • PHP
    • Python
    • .NET
    • Yii
    • Mobile
    • Android
    • iOS
    • React Native
    • Flutter
    • Ionic
    • Kotlin
    • CMS & CRM
    • Spree
    • Magento
    • Wordpress
    • Drupal
    • Umbraco
    • Woocommerce
    • Salesforce
    • Microsoft Dynamics 365
    • Explore All
  • Contact Us
  • CLOSE
Vue.js Best Practices 2020

Vue.js Best Practices 2020 (To Craft Highly Modular Application)

Avatar
Riken Solanki Technical Writer
May 14, 2020 7 min read

Last Updated on August 20, 2020

When it comes to modern front-end development, Vue.js continues to become more and more widespread as its growing sensation and one of the trendiest progressive frameworks for building interactive UIs. However, till the time, my personal favorite was ReactJS. But, when one of my colleagues told me about Vue async components, Vue.js tool, and libraries, I decided to get my codes into it and trust me, it was an immensely rewarding experience.

I discovered interesting patterns, a vast library, outstanding community support, and one of the easiest front-end frameworks to my experience in the comparison of other JavaScript frameworks. Vue.js does not only make the fellow developers fall for it but down the line, you will be thankful for opting Vue.js and making your life so easier.

So, let me get you through the Vue.js best practices that I have learned, and I would like you to give a try.

#1 Make sure to use key Inside v-for

Using key attributes along with the directive can help your app to be predictable and persistent; howsoever small changes you make in the data. This is essential, so it’s convenient to track your components and have an accurate reference to your diverse elements.

Without Keys, there’s no surety about how DOM elements can appear, and there isn’t assurance about their predictable behaviors. But, if you maintain to have a unique key for each component, then it’s easy to predict how Vue application will handle DOM manipulation.

< !-- BAD -- >
< div v-for=features in features' >  < /div >
 
< !-- GOOD! -- >
< div v-for= features in features' :key='feature.id' >

#2 Use Actions: Commit the Data and Make API Calls

I make most of my API calls inside the Vuex actions because they make so much convenient to fetch the data and provide a level of reusability and encapsulation.

  • If I am required to fetch the same web page on two different places, then with the help of the dispatcher and right parameters, I will be able to fetch, commit and return, with no duplicate code than the dispatcher.
  • If I am required to apply the same logic when the page has already been fetched, I will do it one place to decrease the load on the server, and I am pretty sure that it will work everywhere.
  • It is so much convenient to track actions into the Mixpanel events, and on the other hand, the code base is easy to maintain. Trust me; you will have a sense of satisfaction working this way.

#3 Naming Commits – Follow a single Convention

Once the application grows, we all are required to browse the history of the components. If you or any of your team member has not followed the same convention to name their commits, it will become critical to identify and understand what each does. Follow the listed below guidelines to make your commits easier when you are going through the project history.

Commit Message Format

< type >(< scope >): < subject >
< BLANK LINE >
< body >
< BLANK LINE >
< footer >

Sample:
docs(changelog): update changelog to beta.5
fix(release): need to depend on the latest rxjs and zone.js

To understand in detail how it works, go through this guideline.

#4 Multiple V-Condition

It is advisable never to use various v-if conditions to render multiple elements from a Vue component’s render function. Use the < div > to wrap the elements and < template > to wrap the extra element.

< template v-if=“true” >
 < p >Paragraph 1< /p >
 < p >Paragraph 2< /p >
 < p >Paragraph 3< /p >
< /template >

#5 Third-part packages

When a team of developers is working on the same project, the numbers of installed packages become so high that no one care to pay attention to it. It indirectly affects the performance of the application. To improve the performance, you can use import cost packages to have a look at the imported modular library and identify what’s wrong when it is growing.

#6 Stay Consistent – Directive Shorthand

I have seen this practice as a common technique to use shorthand for directives.

  • @ for v-on
  • : for v-bind
  • # for v-slot

One of my favorite practices that I take into consideration. Here, you should be very careful that either you should use them throughout the project or better not use it from the start. This Vue.js practice makes your project more consistent and your code readable.

#7 Code Splitting

Performance matters, and once the application is getting large, it becomes more than important, makes it as fast and as efficient as possible.

Async components

Vue.component('async-component', (resolve) => {
  resolve({
    template: '< div >Async Component< /div >',
    props: [ 'myprop' ]
  });
});

Single file components

< template >
  < div >Async Component< /div >
< /template >
< script >
  export default {
    props: [ 'myprop' ]
  }
< /script >

This syntax for importing is even neater:

new Vue({ 
  el: '#app',
  components: {
    AsyncComponent: () => import('./AsyncComponent.vue')
  }
});

Dynamic Module Loading

export default {
  template: '< div >Async Component< /div >',
  props: [ 'myprop' ]
}

You may try something like this;

import AsyncComponent from './AsyncComponent.js'`;
Vue.component('async-component', AsyncComponent);

#8 Routing

At the time of building SPAs, it is most common to use client-side routing. Vue does not ship on built-in routing as it has official plugin called VueRouter and it so easy to work with and provides all the functionalities that you need to build a powerful application. Incase if you are using the VueCLI, you can add it to your application without installing npm-install Vue-router.

#9 Avoid to Access the DOM Directly

While programming on the Vue application, by all mens you should avoid accessing the DOM directly. Instead you should use $refs, I find it an ideal way to access the DOMand its more maintainable and you are no more required to depend on specific class names.

#10 Add multiple classes to an element

One of the best things about Vue is it makes so convenient to add dynamic class to an element

//Add class red if isError is true
< div :class=”{'red': isError}” >< /div >

A different yet most common and easy approach is;

// Add to classes if two properties return true
< div :class="{'red': isError, 'text-bold': isActive }” >< /div >

#11 kebab-case for event names

At the time of emitting and listening to custom events, make sure to use kebab-case. Kebab-case helps to transfer the events into lowercase. I haven’t seen anyone listening to an event in PascalCase or camelCase, so you should declare the event in a way you will going to listen to it: in kebab-case.

// Emitting
this.$emit('my-event') // instead of myEvent

// Listening
v-on:my-event

#12 Use Selectors

It is better explained with code:

// We have this selector
export const language = (state) => state.userConfig.language;// In one of our actions, we need language:
// Bad
[GET_GAMES]({ commit, rootState }) {
   const lang = rootState.userConfig.language;
   // Do stuff...
}
// Good
[GET_GAMES]({ commit, rootState }) {
   const lang = language(rootState);
   // Do stuff...
}

Source: Github

#13 Validate your Props

I wasn’t sure about this practice easier, I came to know about it very late, but one of the best practices to follow. When you are working on a large-scale project, it is not possible to remember the type, format, and conventions that you have used in a prop. So considering the below example in a similar way, you can write prop validation.

props: {
  status: {
    type: String,
    required: true,
    validator: function (value) {
      return [
        'syncing',
        'synced',
        'version-conflict',
        'error'
      ].indexOf(value) !== -1
    }
  }
}

Source: Vue– Style Guide

#14 Appropriate Structure

Vue has not any default way to structure the application unless you are using Nuxt. So it demands you and your team to be a very organized developer(s) to create an understandable structure, because changing it later is even more stressful and hustle.

#15 Clean up after yourself.

At the time of designing SPA, it is quite obvious to build up memory usage; if you don’t eliminate instances, intervals, custom events, then there are chances that your app will become slow or maybe unresponsive.

This could look something like:

created() {
  refreshUserLoginTokenInterval(); //At created we start an interval to refresh
},
beforeDestroy () {
  destroyUserLoginInterval(); //Before component is destroyed, we clean up
}

To know more about avoiding memory leaks, please go through this cookbook.

Wrapping Up

So, the above listed are the key strengths and Vue.js best practices that I am taking into consideration to create a stunning Vue.js application. I am sure every individual developer would have their own on best practices, so If you think I have missed any of Vue.js best practices that I should cover in this blog post, then feel free to comment below.

If you are looking for the best top-tier Vue.js developers to build a high-octane performance-obsessed real-time application, then hire Vue.js developer from us. We are a globally renowned Vue.js development company, and we have incorporated all the versions of Vue.js from 0.9 to 2.6 as our mainstream front-end technology.

Vue.js Best Practices:FAQs

  • Why is Vue.js so popular?

    First of all, Vue.js is lightweight, modular, flexible and high performant. Due to the more flexible and less opinionated solution, Vue.js performs so smoothly and allows you to structure your application at your convenience instead of being forced like other front-end frameworks. Being one of the trendiest front-end frameworks and the growing developer community worldwide Vue.js is gaining more traction every month.

  • What is Vue used for?

    Vue is an open-source progressive javascript framework that is mostly used to build web interfaces and SPAs. Unlike other front-end frameworks, Vue.js is not backed by big tech companies. Along with the web interfaces, Vue.js is also used for mobile and desktop application development with the help of Electron framework.

  • What are Vue components?

    Vue.js is a component-based approach that allows you to build interactive user interfaces. This is the reason why Vue components are considered as the most powerful features of Vue.js. Components are custom elements that allow you to break your application into smaller and reusable parts to build user interfaces out of the complicated structure.

Avatar
Riken Solanki View all post
Storyteller, Marketer, Developer, Writer, Thinker, Enabler, Rescuer, Wordsmith. I see myself as a Content Strategist. I love to write about anything and everything that pertains to the digital world, as I believe it’s an ever evolving space creative in its own way. I love facing challenges, Blogging and sharing technologies

Expand Your Digital Horizons With Us.

Start a new project or take an existing one to the next level. Get in touch to start small, scale-up, and go Agile.


Or
E-mail us : [email protected]

Your Success Is Guaranteed !


Related articles
Vue js Large Application Structure
Vue.JS
How to Optimize the Performance of Vue js Large Application Structure?
February 10, 2021 by: Archita Nayak
Vue 3 Reactivity
Vue.JS
Let’s Explore the State of Vue 3 Reactivity, JavaScript Reactivity and Composition API
January 8, 2021 by: Archita Nayak
Vue Storefront
Vue.JS
Introduction to Vue Storefront: Headless PWA to Boost Your eCommerce Site Performance
November 11, 2020 by: Paridhi Wadhwani

Top 1% IT Talent

Bacancy Technology is an exclusive hub of top dedicated software developers, UI/UX designers, QA experts, and product managers with an incredibly rare and hidden talents you will ever come across. We let you access the top 1% IT talent from independent software developers to the fully managed teams.

Time Zone Aligned

Timezone is never a constraint when you are working with Bacancy Technology. We follow one very simple principle – our developers and your time zone. Hire dedicated software developers from us and make collaboration in a faraway to work according to your time zone, deadline, and milestone.

Experienced Team

Whether you are looking for skilled developers in emerging technologies or looking for an extended arms to augment your existing team, we can lend a helping hand in both situations. We are a full-stack software development company with 300+ skilled and experienced software developers whom you can hire at your convenience to address the ongoing business challenges

Let us help you build a modern digital business to overcome traditional culture and succeed in the age of digital transformation.

  • USA
  • Canada
  • Australia
  • India
  • UAE
  • Sweden

USA

Bacancy Technology LLC

Florida

4995 NW 72nd Ave, Suite 307 Miami, FL 33166

Phone

+1 347 441 4161

Email

[email protected]

We guarantee 100% security of your information. We will not share the details you provide above with anyone. Your email won't be used for spamming.

Canada

Bacancy Technology Inc

Toronto

71 Dawes Road, Brampton, On L6X 5N9, Toronto

Phone

+1 416 907 6738

Email

[email protected]

We guarantee 100% security of your information. We will not share the details you provide above with anyone. Your email won't be used for spamming.

Australia

Bacancy Technology

South Australia

351A Hampstead Rd, Northfield SA 5085

Phone

(02) 8005 8222

Email

[email protected]

We guarantee 100% security of your information. We will not share the details you provide above with anyone. Your email won't be used for spamming.

India

Bacancy Technology Pvt Ltd

Ahmedabad

1207-1210, Time Square, Thaltej-Shilaj Road, Ahmedabad

Pune

2nd Floor, Marisoft-1, Marigold IT Park, Pune

Phone

079- 40037674

Email

[email protected]

We guarantee 100% security of your information. We will not share the details you provide above with anyone. Your email won't be used for spamming.

UAE

Bacancy Technology

Dubai

1608 Clover Bay, Business Bay, Dubai, UAE. PO Box 62049

Phone

+1 347 441 4161

Email

[email protected]

We guarantee 100% security of your information. We will not share the details you provide above with anyone. Your email won't be used for spamming.

Sweden

Bacancy Technology

Hagersten

Junkergatan 4, 126 53 Hagersten

Phone

+1 347 441 4161

Email

[email protected]

We guarantee 100% security of your information. We will not share the details you provide above with anyone. Your email won't be used for spamming.

How Can We Help?

  • Employee
  • Brochure
  • Quality Assurance
  • Resources
  • Privacy Policy
  • Sitemap
  • Solution
  • Contact Us
DMCA.com Protection Status
Request A Free Consultation