Skip to content
Advertisement

How to initiate a function using data(){} but not mounted(){} while using vue-resource

This works, but I need to use mounted(){} to initiate the function which I think can be avoided but not sure how.

JavaScript

This code doesn’t work, but I like to do something like this. Initiating the function in data(){} itself.

JavaScript

Thanks in advance.

Advertisement

Answer

I agree with Decade Moon that your first approach is the better way to do it (though you could use created instead of mounted).

The reason your second approach doesn’t work is that you return an array and then replace the local variable’s value with a different array. What you need to do is populate the array you returned.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement