Append dynamic vue component after specific element

I want to add component in random position (not random actually, based on some logic) based on click of other elements.

In jQuery, we can achieve that by $('#someId').append('element') or by $('#someId').find('.someClass').after('element')

What I want achieve (jquery version): jsfiddle

How to do that in Vue?

N.B: This couldn't be achieved by v-for as all elements won't appear sequentially or in the same place. Also, components should not be appeared on initialization as this is dynamic.



If this is about arranging that which component will appear after another. Let me assume I have all those components in an array and you can re-arrange those in the array as par your logic, and you can use special attribute: is to render those component.

Here in sample I have an array of components and I use v-for to render those one after another:

See fiddle here.


<div id="app">
  Following are the components
  <div v-for="comp in compArray" :is="comp"></div>
  <button @click="resuffle">

<template id="comp1">
    <span>This is component 1</span>

<template id="comp2">
    <span>This is component 2</span>

<template id="comp3">
    <span>This is component 3</span>


Vue.component('comp1', {
  template: '#comp1', 

Vue.component('comp2', {
  template: '#comp2', 
Vue.component('comp3', {
  template: '#comp3', 

new Vue({
    el: '#app',
  data: {
    compArray: ['comp1', 'comp2', 'comp3']
  methods: {
    resuffle: function() {

Can you provide an example of your parent component when you are traversing your array/list to map your element with a component? (just to understand your use case)

You could use v-for but on multiple arrays generated by some computed properties in the place you want to display them (if the places you wanna display components are not also randomly chosen).


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.