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

86
Visualizações
Appending multiple Vue2 components from ajax html response not working

I'm trying to append multiple vuejs components with jquery ajax, but it's not working. It all works fine, until response returns more than one component, or component within component.

Here's the code:

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<div id="app">
</div>

<script type="text/x-template" id="template-1">
    <div>
     <h1>Template 1 {{param}}</h1>
    </div>
</script>

<script type="text/x-template" id="template-2">
    <div>
     <h1>Template 2 {{param}}</h1>
    </div>
</script>


<script>
    Vue.component('template-1', {
         template: '#template-1',
         props: ['param']
       });

       Vue.component('template-2', {
         template: '#template-2',
         props: ['param']
       });

       const vm = new Vue({
           el: '#app'
       });
</script>

<script>

    function loadMore(){
        $.get('/home/test', function (response) {
            var MyComponent = Vue.extend({
                template: response
            });

          
            var compiled = new MyComponent().$mount();
            $('#app').append(compiled.$el);
        });
    }

   loadMore();

</script>

If the following response comes from ajax, it works, and renders one component:

<template-1 param="Test"></template-1>

If the following response returns from ajax, it renders only first component:

<template-1 param="Test"></template-1>
<template-2 param="Test"></template-2>

If the following response returns from ajax, it renders both components:

<div>
    <template-1 param="Test"></template-1>
    <template-2 param="Test"></template-2>
</div>

If the following response returns from ajax, it renders only parent component:

<template-1 param="Test">
    <template-2 param="Test"></template-2>
</template-1>

Is there a way to make this work always, without knowing how many components will be returned from the server?

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

0

Since you are telling Vue that your response.data should be the value of the el property, it will look inside it and render all components that were registered and therefore known.

In your case you should always wrap your response inside another HTML element, which then should be the value of the el property.

Notice: Vue elements inside other Vue elements can only be rendered, if the parent component (here your template-1) has registered the second one itself. Means when template-1 got rendered, everything inside it will be removed, except it is using slots.

If you want to use slots, this could help you.

Otherwise you could use the render function and build your own dynamic render stuff.

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