6

According to the docs, the constructor of the Vue object is managed like this.

var vm = new Vue({
  created: function () { console.log("I'm created!"); }
});

However, I can't figure out how to do the corresponding thing when a Vue component is created. I've tried the following but don't get any print to the console.

export default {
  created: function() { console.log("Component created!"); }
}

Is it possible to subscribe/listen to a component being rendered? I'd like to react to that event by downloading some data and putting it in the store, so that the table that the component carries will get its information to display.

6
  • is your export default inside of a .vue file as part of a webpack project? there at some point needs to be a call to make a root vue instance that you import components created in .vue files. Commented Dec 10, 2016 at 9:55
  • @vbranden To answer your questions - the export is inside a .vue file and I do manage my bundling/running with webpack. The other part, I didn't undertand. Could you explain, please? Commented Dec 10, 2016 at 9:59
  • maybe I'm not understanding your problem. You have a root Vue instance and a component that has a console.log inside its created hook. Are you at some point in your project importing that component and using it? if so you should see the console log Commented Dec 10, 2016 at 10:08
  • @vbranden Yes. I must have done something retarded last night. It seems to perform as I expected it it. You're right... If you repost your comment with link and some short info on the other methods (besides created), I'll be happy to accept it and even grant +1. Commented Dec 10, 2016 at 10:16
  • did you solved this problem? Commented Apr 12, 2019 at 15:02

2 Answers 2

8

In my applications, I tend to use the mounted hook to load up some Ajax data once the component has mounted.

Example code from my app:

Vue.component('book-class', {
    template: '#booking-template',
    props: ['teacherid'],
    data: function () {
        return{
            // few data items returned here..
            message: ''
        }
    },
    methods: {
        // Few methods here..
    },
    computed: {
      // few computed methods here...
    },
    mounted: function () {


        var that = this;
        $.ajax({
            type: 'GET',
            url: '/classinfo/' + this.teacherid,
            success: function (data) {
                console.log(JSON.stringify(data));

            }
        })
    }
});


new Vue({
    el: '#mainapp',
    data: {
        message: 'some message here..'
    }
});

However, I can also use created() hook as well as it is in the lifecycle as well.

In Vue2 you have the following lifecycle hooks:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

That's only applicable upon creation/hookation of the Vue main component. I'm looking for a way to do stuff when a component (any component, part of or child to) is created/imported. Still +1 for the effort with the code. Also, I'd remove the image and just link to it. It's awesomely helpful but ouch so looong.
1

components doesn't have life cycle hooks like app. but they has something similar. that fixed my problem: https://v2.vuejs.org/v2/api/#updated

2 Comments

Ahem... What would you say about the picture above that shows life cycle of an component? AM I missing something here?
@KonradViltersten picture above is lifecycle of app not lifecycle of component. Comenents doesn't have those hooks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.