Event custom event from the main vue instance, listening from the component

Pretty new to vue js here,

I just want to know if it is possible to issue a custom event from the main instance of vue (parent), and the components can listen to this custom event and act accordingly when this event is fired?

I want to fire a custom event in vive instance lifecycle mode mounted , then all components can initialize themselves.

ex.

The main instance of vue

new Vue( {
    el : '#main-app',
    mounted : function() {
       this.$emit( 'init-app' );
    }
} );

Then, on different components, it can listen to the custom 'init-app' event , and then act accordingly when it is triggered or emitted.

, vue js. html? ?

. vue js 2.0.3 ,

.

Vue.component('my-component', {
   template: '#component-template'
});

.

.

+4
1

created: .

: , init-app . , .

. , this.$parent.$emit("my-event-code") this.$on("my-event-code"). , .

, init-app , , this.$parent.$on("init-app", ...) - , .

+4

Source: https://habr.com/ru/post/1657949/


All Articles