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

264
Visualizações
Vue 3 component stops working when included in a parent component's template

I'm working with a very simple Vue 3 app, listed below.

Due to the nature of our project, we want use in-DOM templates.

This works fine for a single component. But once a child component is added, this component does not work.

See the example below, where SubComponent stops working when we enable the createApp for the App component.

I've tried using slots, experimented with registering as components, but none of these work.

What am I overlooking?

const {ref} = Vue;

const SubComponent = {
  setup() {
    const text = ref("");
    return {
      text
    };
  }
};

const App = {
  setup() {
    const count = ref(0);
    const inc = () => {
      count.value++;
    };

    return {
      count,
      inc
    };
  }
}


Vue.createApp(SubComponent).mount("#subcomponent");
Vue.createApp(App).mount('#app')
<head>
  <script src="https://unpkg.com/vue@3.2.29/dist/vue.global.prod.js"></script>
</head>

<body>
  <div id="app">
    <h1>Hello Vue 3!</h1>
    <button @click="inc">Clicked {{ count }} times.</button>
    <div id="subcomponent">
      <h2>Hello from subcomponent</h2>
      <input v-model="text" />
      <br />
      <span>{{text}}</span>
    </div>
  </div>
</body>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Maybe like following snippet:

const {ref} = Vue;

const SubComponent = {
  template: `
    <h2>Hello from subcomponent</h2>
    <input v-model="text" />
    <br />
    <span>{{text}}</span>
  `,
  setup() {
    const text = ref("");
    return { text };
  }
};

const App = {
  setup() {
    const count = ref(0);
    const inc = () => {
      count.value++;
    };
    return { count, inc };
  }
}
Vue.createApp(App)
  .component('SubComponent', SubComponent)
  .mount('#app')
<head>
  <script src="https://unpkg.com/vue@3.2.29/dist/vue.global.prod.js"></script>
</head>

<body>
  <div id="app">
    <h1>Hello Vue 3!</h1>
    <button @click="inc">Clicked {{ count }} times.</button>
    <sub-component></sub-component>
  </div>
</body>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't nest multiple vue apps, you only have one app and within it you can have multiple or nested components. Not sure with the approach having everything in one file though.

const {ref} = Vue;

const SubComponent = {
  setup() {
    const text = ref("");
    return {
      text
    };
  }
};

const App = {
  components: {
    SubComponent,
  },
  setup() {
    const count = ref(0);
    const inc = () => {
      count.value++;
    };

    return {
      count,
      inc
    };
  }
}


Vue.createApp(App).mount('#app')
<head>
  <script src="https://unpkg.com/vue@3.2.29/dist/vue.global.prod.js"></script>
</head>

<body>
  <div id="app">
    <h1>Hello Vue 3!</h1>
    <button @click="inc">Clicked {{ count }} times.</button>
    <sub-component>
      <h2>Hello from subcomponent</h2>
      <input v-model="text" />
      <br />
      <span>{{text}}</span>
    </sub-component>
  </div>
</body>

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