Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
Vue3: How to mount a component into an existing App?

Let's say that we have an App was already mounted, in main.js:

createApp(App).mount('#app');

And I'd like to write a function, which should be called like this:

createModal({
  render: () => <SomeComponent />,
});

Normally when we implement functions like above, we just call the createApp(h(render)) function, and then mount its instance in anywhere we want.

But the problem is that when we did, the modal instance won't inherit anything from the root App.

So if we have something like provide('root', 'here') was processed in the root App, and then we call inject('root') in the other App, we won't get the string result here.

Instead, the result will be undefined, Because these two Apps are not sharing the same context.

What should I do to make things right?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's what you could do:

const { createApp, reactive, toRefs, defineComponent, computed } = Vue;

const state = reactive({
  foo: 'bar'
})

createApp({
  setup() {
    const addDynamicApp = () => {
      const div = document.createElement('div');
      document.querySelector('#app').appendChild(div);
      createApp({
        setup: () => ({
          ...toRefs(state)
        }),
        template: `<h3>Dynamic child (new App)</h3>
                   <input v-model="foo">`
      }).mount(div);
    };
    return {
      ...toRefs(state),
      addDynamicApp
    }
  }
}).mount('#app')
#app {
  border: 2px solid #f50;
  padding: 1rem;
}
<script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script>
<div id="app">
  <input v-model="foo" />
  <button @click="addDynamicApp">Add dynamic app</div>
</div>


The underlying principles are:

  • any app can read/write from/to a reactive state (...toRefs(state), in both)
  • an app can be mounted in any <div>, even if that div happens to be part of another app.

You can have as many apps, sharing the same reactive state. They can be completely separate in DOM or nested into one another.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda