Watch a specific prop of an object

How do you watch a specific prop of an object? Is it useful at all? Watch is invaluable. It allows us to react to changes in the state and keep it that way through the life cycle of the component. Creating a watch is pretty simple. Consider the below example where we are monitoring the search text to respond to changes. You may observe that we simply do not respond to changes, but debounce it - i.e., wait for 1000ms and trigger off a request only if there was no user input within the 1000ms. ...

Measure Performance in Vue

Measure your Vue application performance in a standard and consistent way. Previously we have seen how we can quickly measure performance in Javascript. How about Vue? Vue is the right mix of UI and business logic, and cannot be tested with standard back-end script execution speed. On the other hand, standard UI testing tools may prove to be a bit of an overkill due to the effort required to develop and maintain them. ...

Bundle size using Vue CLI

You can get a bundle size report just by using Vue CLI and nothing else. Keeping track of bundle sizes is a best practice. You need to know what gets delivered to users so that you can try to optimize stuff and take corrective steps to decrease chunk sizes, tree-shake where you can etc. All these steps will hopefully lead to smaller bundles or smaller incremental files that should speed up your application (at least in perception!). ...

Lazy load images in Vue

Lazy load images to improve perceived performance. If you have a view that has lot of images, you may observe high initial page load time and stuttering. Vue has to get the comparably larger payload to paint the UI, and this takes time. The easiest way of lazy loading images is by using an package called vue-lazyload. First, install the package - npm install vue-lazyload Next, load this with Vue. // main.js import Vue from "vue"; import VueLazyload from "vue-lazyload"; // other code Vue.use(VueLazyload); Finally, use lazy loading in your components. ...

Make an object non-reactive in Vue

Are you tired of Vue reactivity? Do you long for days when you had to manually refresh HTML pages to see changes? Or, do you have large objects and reactivity is killing your app? Vue has the answer. Vue enables this magic to respond to any changes in state on the UI. You can just declare variables in a component, and low and behold - they are reactive. <script> export default { data() { return { accounts:[], }; }, mutations: { setAccounts(state, accounts) { state.accounts = accounts }, addAccount(state, account) { state.accounts.push(account); } }; } </script> In the above example, accounts is reactive. Any changes to accounts is reflected on the UI without needing any other changes or logic. ...

Remember position history and navigate to anchors in Vue

You can remember the position in a view and navigate users to that position when they click on ‘back’. You could also navigate users to specific anchor links in a long page. Vue helps you to create a great single page application. Navigation between views is almost seamless and “feels” really fast. This magic of front-end routing is enabled by the Vue router. A simple router code-fragment looks like this - ...

How to break reactivity in Vue for arrays and objects?

Let us move to the dark side and break some things. We will start with Vue’s reactivity. See a couple of commonly used patterns to break reactivity in arrays and objects. And oh, there are ways of working around that if you are into boring stuff. All the below things applicable to Vue 2 btw. Direct Array Assignments Let us try out with the following code - <!DOCTYPE html> <html> <head> <title>Test Reactivity</title> <script src="https://unpkg.com/vue"></script> </head> <body> <div id="app" style="text-align: center"> <button @click="addToNumPush()"> Add to Nums (Push) </button> <button @click="addToNumDirect()"> Add to Nums (...) </button> {{ nums }} </div> <script type="text/javascript"> var app = new Vue({ el: "#app", data: { counter: 0, nums: [1, 3] }, methods: { addToNumPush() { this.nums.push(this.counter++); console.log(this.nums); }, addToNumDirect() { this.nums[10] = 99; console.log(this.nums); } } }); </script> </body> </html> As you can see, the first button makes changes to the array nums and those changes are immediately available on button click. The second button does not seemingly make any changes. However, you will see from the console statement in developer tools that the array has indeed changed. ...

A shorthand to call same method in created and watch

Use a less known property called immediate to call a function during created and in watch. Take the below scenario - Component needs to fetch data from server to get contact detail when user clicks on a specific contact in a list User can use arrow keys to fetch next or previous contact by clicking arrows on the component One of the typical ways to address this requirement is to call the fetchContact method ...

Type cast in style in a Vue component

Do you type cast in Vue? Of course you do. So, is there a way to just not use Javascript shortcuts since they are so cumbersome? Really? You find this cumbersome? let myNumStr = +myNum; let myStrNum = "" + myStr; See more at type casting in Javascript and shortcuts to type cast. Regardless of your opinion, with Vue you now have the option to see more.. options. See below - <template> <input v-model.number="position" type="number" /> </template> <script> export default { data() { return { position: 0 }; } }; </script> Bad news if you wanted to go overboard with this - number is literally the only thing supported here. So, back to the cumbersome Javascript type conversions for rest of the use cases. ...

Extend a component to create cool stuff

Vue enables you to extend a component, thereby carrying forward all the base elements and add some more. This is one of the efficient ways for component composition. Let’s say you have a simple panel that you use everywhere - <!-- MainPanel.vue --> <template> <!-- main area --> <!-- panel footer --> </template> Now, you get this super idea to include a title in the panel. Normally you would just have a tag that conditionally renders based on prop and thereby control whether title shows up or not. ...