Dynamic html elements in Vue.js Dynamic html elements in Vue.js vue.js vue.js

Dynamic html elements in Vue.js


Update: Based on this answer, you can do a similar dynamic-template component in Vue 2. You can actually set up the component spec in the computed section and bind it using :is

var v = new Vue({  el: '#vue',  data: {    message: 'hi #linky'  },  computed: {    dynamicComponent: function() {      return {        template: `<div>${this.hashTags(this.message)}</div>`,        methods: {          someAction() {            console.log("Action!");          }        }      }    }  },  methods: {    hashTags: function(value) {      // Replace hash tags with links      return value.replace(/#(\S*)/g, '<a v-on:click="someAction">#$1</a>')    }  }});setTimeout(() => {  v.message = 'another #thing';}, 2000);
<script src="//unpkg.com/vue@latest/dist/vue.js"></script><div id="vue">  <component :is="dynamicComponent" /></div>