JSFiddle - React, Tailwind, and code Playground

by robert chang

HTML

<script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script>
<div id="app">
  by dynamic Component:
  <component
    v-for="item in items"
    :is="item.component"
    :opts="item.options">
  </component>

  <br /> by directly ref Component:
  <node :opts="items[0].options">
  </node>

  <br />


  <div>
    Select a component type:
  </div>
  <div>
    <label for="newItemNode">Node</label>
    <input id="newItemNode" type="radio" value="node" v-model="newItem.component">
  </div>

  <div>
    <label for="newItemNode2">Node 2</label>
    <input id="newItemNode2" type="radio" value="node2" v-model="newItem.component">
  </div>
  <div>

    <div>
      Enter your options:
    </div>

    <label for="newItemOptions">Options</label>
    <input id="newItemOptions" @keyup.enter="addItem" type="text" v-model="newItem.options">
  </div>

  <button v-if="isButtonDisplayed" @click="addItem">new item</button>
</div>

JavaScript

//http://forum.vuejs.org/topic/349/injecting-components-to-the-dom/3

Vue.component('node', {
  template: "<div>node: {{ opts }} {{ log }}</div>",
  props: ['opts'],
  computed: {
    log: function() {
      return JSON.stringify(this.opts);
    }
  },
  data() {
    return {}
  }
});

Vue.component('node2', {
  template: "<div>node2: {{ opts }} {{ log }}</div>",
  props: ['opts'],
  computed: {
    log: function() {
      return JSON.stringify(this.opts);
    }
  },
  data() {
    return {}
  }
});


new Vue({
  el: '#app',

  data() {
    return {
      newItem: {
        component: "",
        options: ""
      },
      items: [{
        component: "node",
        options: 'xxxx'
      }, {
        component: "node2",
        options: 'xxxy'
      }]
    };
  },
  computed: {
    isButtonDisplayed() {
      return this.newItem.component && this.newItem.options
    }
  },
  methods: {
    addItem() {
      this.items.push(this.newItem);
      this.newItem = {
        component: "",
        options: ""
      }
    }
  }
});