Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a function when a component is created in Vue?

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.

like image 389
Konrad Viltersten Avatar asked Dec 09 '16 16:12

Konrad Viltersten


People also ask

How do you call a Vue component?

If we want to call a method in the parent component from the child component, then we should call the $emit method from the child component and listen to the event emitted by the child component in the parent component. In HelloWorld. vue , we have the greet method which calls this.

How do I render a component in Vue?

Each Vue component implements a render function. Most of the time, the function will be created by the Vue compiler. When you specify a template on your component, the content of this template will be processed by the Vue compiler that will return a render function.

What is a functional component in Vue?

Functional components defined as a Single-File Component in a *. vue file also receives proper template compilation, Scoped CSS and hot-reloading support. To denote a template that should be compiled as a functional component, add the functional attribute to the template block.


2 Answers

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

like image 174
Sankalp Singha Avatar answered Sep 29 '22 03:09

Sankalp Singha


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

like image 34
Milan MiNo Michalec Avatar answered Sep 29 '22 03:09

Milan MiNo Michalec