Vue on ready or installed events for the entire application and all its components
P粉052686710
2023-08-26 16:27:47
<p>I was wondering if there is a way to check if the enitre Vue app is installed? </p>
<p>I'm loading a dialog script that checks certain links on the page and adds a dialog event to them... but the problem is that it runs too early when the page loads. Use jQuery's .ready() function. But not all components are installed at this point... and some Vue component links don't have dialog link events attached. </p>
<p>I want to be able to do something like this:</p>
<pre class="brush:php;toolbar:false;">$( document ).ready( function () {
const app = createApp();
app.component( 'section-header', SectionHeader );
// more components etc...
const mountedApp = app.mount( '#app' );
if (mountedApp.ready()) {
// now load my custom non-vue dialog script so we are sure the DOM AND the all components are mounted.
let CsDialog = require( './vendor/cs-dialog.min' );
dialog = new CsDialog();
dialog.bindEvents();
}
});</pre></p>
You don't need
jQuery
at all.Application
mounted()
/onMounted()
The hook will run after all components have been mounted.See playground below.
Application hooks run at the end.
See more about Lifecycle Hooks and
onMounted()