Vue: dynamically rearrange children of a component

Created on 26 Apr 2016  ·  1Comment  ·  Source: vuejs/vue

i have a vue component with children as below , id like to rearrange the children when a certain event occurs , how would i achieve this ?

<template>
    <div>
       <child-one></child-one>
       <child-two></child-two>
    </div>
</template>
<script>
   module.exports = {

      components: {
         'child-one' : require('./child1.vue'),
         'child-two' : require('./child2.vue')

      },
    events: {
         'rearrange': function (d) { 
            // dynamically rearrange 'child-one' and 'child-two'
          }
   }
</script>

Most helpful comment

Use an array to drive the children:

<component v-for="type in comps" :is="type"></component>
module.exports = {
      data: function () {
          return { comps: ['child-one', 'child-two'] }
      },
      components: {
         'child-one' : require('./child1.vue'),
         'child-two' : require('./child2.vue')

      },
    events: {
         'rearrange': function (d) { 
            this.comps.reverse()
          }
   }

>All comments

Use an array to drive the children:

<component v-for="type in comps" :is="type"></component>
module.exports = {
      data: function () {
          return { comps: ['child-one', 'child-two'] }
      },
      components: {
         'child-one' : require('./child1.vue'),
         'child-two' : require('./child2.vue')

      },
    events: {
         'rearrange': function (d) { 
            this.comps.reverse()
          }
   }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

guan6 picture guan6  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments

loki0609 picture loki0609  ·  3Comments

paceband picture paceband  ·  3Comments

aviggngyv picture aviggngyv  ·  3Comments