Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js does an event trigger after component has been rendered?

Tags:

I've got some custom components in Vue.js. In one of the components I have is a select list that I want to render as a Chosen select box. I use this with a jQuery function $("select").chosen().

<template v-for="upload in uploads">     <new-upload-container :index="$index" :upload="upload" v-if="isNewUpload"></new-upload-container>     <existing-upload-container :index="$index" :upload="upload" v-if="isExistingUpload"></existing-upload-container> </template> 

After I add data to the uploads bound array in the Vue instance, the view updates with an instance of the component. Unfortunately when I try to instantiate Chosen on the select field, it doesn't work.

I'm not sure if it takes a short time after I add an item to the uploads bound array that the DOM actually updates.

<template id="existing-upload-container">        <select name="beats[@{{ index }}][existing_beat]" class="existing-beats">            <option selected="selected" value="">-- Select a linked beat --</option>                   @foreach ($beats as $beat)                    <option value="{{$beat->id}}">{{$beat->name}}</option>                   @endforeach        </select>      </template> 

Is there an event that is triggered after a component has been fully rendered?

like image 927
Wasim Avatar asked Jan 25 '16 16:01

Wasim


People also ask

How do I pass a Vue event?

To pass event and argument to v-on in Vue. js, we can call the event handler method with $event and whatever argument. And then we can retrieve the arguments from the method parameters in the same order. to call addToCart with the $event object and the ticket.id property.

How does Vue rendering work?

The Vue compiler automatically hoists their vnode creation calls out of the render function, and reuses the same vnodes on every render. The renderer is also able to completely skip diffing them when it notices the old vnode and the new vnode are the same one.

What is the life cycle of Vue component?

Each Vue component instance goes through a series of initialization steps when it's created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes.

What does $emit do in Vue?

Vue $emit is a function that lets us emit, or send, custom events from a child component to its parent. In a standard Vue flow, it is the best way to trigger certain events.


1 Answers

You could try two things in your component, based on which one works with your package. In the Vue object:

{     ready:function(){         // code here executes once the component is rendered         // use this in the child component     },     watch: {         uploads:function(){             //code here executes whenever the uploads array changes              //and runs AFTER the dom is updated, could use this in             //the parent component         }     } } 
like image 92
Jeff Avatar answered Sep 22 '22 16:09

Jeff