Created
April 28, 2023 19:58
-
-
Save epreston/79415482f15e55da25ad6527049bee1d to your computer and use it in GitHub Desktop.
vue - add app.mount lifecycle hooks - onBeforeMount and onAfterMount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add app mount lifecycle hooks to the app instance for plugins to use when registering. | |
function addAppMountLifecycleHooks(app) { | |
const beforeHooks = []; | |
const afterHooks = []; | |
const boundMount = app.mount.bind(app); | |
app.mount = (async (rootContainer, isHydrate, isSVG) => { | |
for (const preHook of beforeHooks) await preHook(app); | |
boundMount(rootContainer, isHydrate, isSVG); | |
for (const postHook of afterHooks) await postHook(app); | |
afterHooks.length = beforeHooks.length = 0; | |
}).bind(app); | |
app.onBeforeMount = (hook, remove = false) => { | |
remove ? beforeHooks.unshift(hook) : beforeHooks.push(hook); | |
}; | |
app.onAfterMount = (hook, remove = false) => { | |
remove ? afterHooks.unshift(hook) : afterHooks.push(hook); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: used when creating the app instance to add hooks that custom plugins can use.
The methods added are:
Each hook is called with the current vue
App
instance either before or after.