Run component method on load vue.js

Zak A picture Zak A · Nov 18, 2016 · Viewed 35.3k times · Source

hey I'm pretty new to Vue.js and I'm trying to accomplish what seems to be a simple thing but I'm, having trouble. Essentially, I need it so every time a component is loaded into the DOM, one of it's methods fire. Here is my current code, I've tried to use v-on:load but it doesn't seem to work.

The same code functions fine if I use the v-on:click event

Answer

str picture str · Nov 18, 2016

There are instance lifecycle hooks that you can use for that. For example:

Vue.component('graph', {
    props:['graphId','graphData'],
    template: '<canvas></canvas>',
    created: function () {
        alert('{{graphId}}');
    },
    methods: {}
});