5 common problems with Vue.js

Fotis Adamakis
Vue.js Developers
6 min readMar 2, 2019

--

After working with Vue.js for a while now I have noticed that everyone comes across the same pitfalls when starting out. That’s why I gathered the most common and hopefully, I can save you some time before hunting StackOverflow for an answer next time.

1. Why is “this” undefined?

Referencing thisin any of the lifecycle hooks (created, mounted, updated, etc.) it evaluates to undefined

Why is this evaluating to undefined in these cases?

Both of those examples use an arrow function () => { }, which binds this to a context different from the Vue instance.

As per the documentation:

Don’t use arrow functions on an instance property or callback (e.g. vm.$watch('a', newVal => this.myMethod())). As arrow functions are bound to the parent context, this will not be the Vue instance as you’d expect and this.myMethod will be undefined.

In order to get the correct reference to this as the Vue instance, use a regular function:

Alternatively, you can also use the ECMAScript 5 shorthand for a function:

2. Make VueJS and jQuery play nice

The way to make Vue play nicely with other DOM-manipulating toolkits is to completely segregate them: if you are going to use jQuery to manipulate a DOM widget, you do not also use Vue on it (and vice-versa).

A wrapper component acts as a bridge, where Vue can interact with the component and the component can manipulate its internal DOM elements using jQuery (or whatever).

jQuery selectors outside of lifecycle hooks are a bad code smell. Your validatePhoneNumber uses a selector and a DOM-manipulating call, but you are using Vue to handle keydown events. You need to handle everything on this widget with jQuery. Don't use Vue to set its class or phone_number or handle its events. Those are all DOM manipulations. As I mentioned, if you wrap it in a component, you can pass props to the component and from those props, you can use jQuery to set class and phone_number.

3. Update parent data from the child component

From the documentation:

In Vue.js, the parent-child component relationship can be summarized as props down, events up. The parent passes data down to the child via props, and the child sends messages to the parent via events. Let’s see how they work next.

How to pass props

Following is the code to pass props to a child element:

How to emit an event

HTML:

JS:

4. Returning Promises from Vuex actions

Actions in Vuex are asynchronous. The only way to let the calling function (initiator of action) to know that an action is complete - is by returning a Promise and resolving it later.

Here is an example: myAction returns a Promise, makes an HTTP call and resolves or rejects the Promise later - all asynchronously

Now, when your Vue component initiates myAction, it will get this Promise object and can know whether it succeeded or not. Here is some sample code for the Vue component:

As you can see above, it is highly beneficial for actions to return a Promise. Otherwise, there is no way for the action initiator to know what is happening and when things are stable enough to show something on the user interface.

And a last note regarding mutators - as you rightly pointed out, they are synchronous. They change stuff in the state, and are usually called from actions. There is no need to mix Promises with mutators, as the actions handle that part.

5. Share data between different pages

You can pass the sharedData as an URL param : 'window.location.href="pageB.html?sharedData=myData"' and access it in the other page. If you would have been using vue-router, you could just do

As you are not using it, You can just use plain javascript to get it, however, you will have to write a helper function like following to get the params, as explained here

Vue way

You can use centralised state management to manage your shared data, which will be available across page unless you reload the page.

You can define your state, like the following:

and you can use it in your components like the following:

A Better Vue way: Vuex way

However, Vue ecosystem has a dedicated redux like state management system as well, called vuex which is quite popular among the community. You store all your projects/users etc in vuex state, and each component can read/update from the central state. If it's changed by one component, an updated version is available to all components reactively. You can initiate the data in one place using actions in vuex itself.

Following is the architecture diagram:

You can have a look at the official vuex documentation for examples on how to call API and save data in the store.

Source: Stackoverflow

--

--

Fotis Adamakis
Vue.js Developers

« Senior Software Engineer · Author · International Speaker · Vue.js Athens Meetup Organizer »