loading...
Cover image for 10 Tips & Tricks to make you a better VueJS Developer

10 Tips & Tricks to make you a better VueJS Developer

simonholdorf profile image Simon Holdorf Updated on ・4 min read

Introduction

I really love working with VueJS and every time I do work with a framework I enjoy digging deep into its capabilities and features. With this post I present you 10 cool tips and tricks you might not have been aware of yet and try to help you become better Vue Devs.

Slot syntax made beautiful

With the rollout of Vue 2.6 a shorthand notation for slots has been introduced that can be used like for events (e. g. @click for the v-on:click event) or colon-notation for bindings (:src). If you for example had a Table-Component you can use this feature as follows:

<template>
  ...
  <my-table>
    <template #row="{ item }">
      /* some content here. You can freely use 'item' here */
    </template>
  </my-table>
  ...
</template>
Enter fullscreen mode Exit fullscreen mode

The $on(‘hook:’)

This is a nice feature you can use if you want to define a custom event listener or third party plugin in the created or mounted hook and need to remove it in the beforeDestroy hook to not cause any memory leaks. With the $on(‘hook:’) method you can use defining/removing the event in just one lifecycle method instead of two.

mounted() {
 const aThirdPartyPlugin = aThirdPartyPlugin()
 this.$on('hook:beforeDestroy', () => {
  aThirdPartyPlugin.destroy()
 })
}

Enter fullscreen mode Exit fullscreen mode

Prop Validation

You probably already know that you can validate your props to be primitives like String, Number or even Object. But you can also make use custom validators, for example if you want to validate against a List of Strings:

alertType: {
 validator: value => ['signup', 'login', 'logout'].includes(value)
}
Enter fullscreen mode Exit fullscreen mode

Dynamic directive arguments

One of the coolest features of Vue 2.6 is the possibility to pass down directive arguments to a component dynamically. Imagine you have a Button-Component and want to listen to a Click-Event in some cases but to a DoubleClick-Event in other cases. That's where those directives come in handy:

<template>
  ...
  <aButton @[someEvent]="handleSomeEvent()"/>
  ...
</template>

<script>
  ...
  data(){
    return{
      ...
      someEvent: someCondition ? "click" : "dblclick"
    }
  },

  methods:{
    handleSomeEvent(){
      // handle some event
    }
  }
  ...
</script>

Enter fullscreen mode Exit fullscreen mode

And whats also really neat --> you can apply the same pattern to dynamic HTML attributes, props and much more!

Reusing a component for the same Route

Sometimes you have different routes which share some components. If you switch between those routes, by default, the shared component will not re-render because Vue is reusing that component for performance reasons. But if you would want to have those components re-render anyway you can do so by providing a :key prop to the Router-View-Component.

<template>
 <router-view :key="$route.fullPath"></router-view>
</template>
Enter fullscreen mode Exit fullscreen mode

All props from parent to child

This is a really cool feature allowing you to pass down allprops from a parent component to a child component. This is especially handy if you have a Wrapper-Component for another component. So instead of passing down all props one by one you can make use of this and pass down all props at once:

<template>
  <childComponent v-bind="$props"/>
</template>
Enter fullscreen mode Exit fullscreen mode

instead of:

<template>
  <childComponent  :prop1="prop1"
                   :prop2="prop2"
                   :prop3="prop3"
                   :prop4="prop4"
                   ...
  />
</template>
Enter fullscreen mode Exit fullscreen mode

All event listeners from parent to child

If you have a child component that is not at the root of the parent component you can pass down all event listeners from the parent to the child likes this:

<template>
  <div>
    ...
    <childComponent v-on="$listeners"/>
    ...
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

In case the child component is at the root of its parent it will get those by default and you do not need this little trick.

$createElement

Each Vue-Instance has by default access to the $createElement method to create and return virtual nodes. This can be utilized for example to use markup in methods that can be passed via the v-html directive. In functional components this method can be accessed as the first parameter in the render function.

Using JSX

Since Vue CLI 3 it supports the use of JSX by default so that you can now write your code in JSX if you like to (and maybe come from React) which comes in handy for example for writing functional components. If you are not on Vue CLI 3 yet you can make use of babel-plugin-transform-vue-jsx to have JSX-support!

The custom v-model

V-model is by default what we call the syntactic sugar over @input event listeners and :value props. But you can specify a "model" property in your Vue-Component to define what event and value prop is used - neat!

export default: {
 model: {
  event: 'change',
  prop: 'checked'
 }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope I could give you some tips for you to help you become a better VueJs Developer. If you are also into react Development you should check out my post 10 Tips & Tricks that will make you a better ReactJS Dev. Feel free to leave a comment and follow me for more upcoming posts!

Discussion

pic
Editor guide
Collapse
steelvoltage profile image
Brian Barbour

I just started using Vue.js, being experienced with React, and am really enjoying it! So these are some well received tips. Thanks.

Collapse
simonholdorf profile image
Simon Holdorf Author

Thanks Brian, Vue Is really cool but so is React of course. Im glad I could give you some insights - this is why I make those articles :)

Collapse
maksym_cossack_developer profile image
Maksym Yankivskyy

Thank you very much i optimise my code with your tips it's very cool, thanks!