Can app instance share plugin and components without app.use() or app.component() again? #13522
Replies: 1 comment
|
Put the registrations in one setup function and call it for each application. Global components and installed plugins belong to an application; copying Here is a small complete example using the public APIs: import { createApp, h, inject, resolveComponent } from 'vue'
const SharedBadge = {
setup() {
const label = inject('label')
return () => h('span', label)
}
}
function createLabelPlugin(label) {
return {
install(app) {
app.provide('label', label)
}
}
}
function createConfiguredApp(Root, label) {
const app = createApp(Root)
app.component('SharedBadge', SharedBadge)
app.use(createLabelPlugin(label))
return app
}
const Root = {
render() {
return h(resolveComponent('SharedBadge'))
}
}
const first = createConfiguredApp(Root, 'First app')
const second = createConfiguredApp(Root, 'Second app')
first.mount('#first')
second.mount('#second')This needs For vue-i18n or other third-party plugins, follow that plugin's multi-app rules; create an instance per app when required. This example creates its plugin per app and does not assume that an arbitrary plugin instance can be shared. I ran the example with Vue 3.5.14 in jsdom: both apps resolved the registered component, each received its own injected label, and unmounting one left the other functional. See app.component and app.use. |
Uh oh!
There was an error while loading. Please reload this page.
now i do it in this way
Is there an easy or ideal approach to achieve this? Thank you.
All reactions