Skip to content
Advertisement

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:

JavaScript

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

JavaScript

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

JavaScript

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

JavaScript

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

JavaScript

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement