Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda