V
V
Vladimir Golub2021-03-24 16:57:37
Vue.js
Vladimir Golub, 2021-03-24 16:57:37

How to replace shortcodes with components?

How to replace shortcode with component after parsing and substitution ?

import Vue from 'vue';
  import TitleElementNew from '@/components/elements/TitleElementNew';

  export default {
    name: "ContentArticle",
    data: function () {
      return {
        text: '<p>Hello world {sample}</p>',
      }
    },
    methods: {
      replaceShortCodes(text) {
        text = text.replace('{sample}', `<TitleElementNew>H</TitleElementNew>`);

        return text;
      }
    },
    render: function(createElement) {
      const template = this.replaceShortCodes(this.text)

      const component = Vue.component('cont', {
        template: template,
        components: {
          'TitleElementNew': TitleElementNew
        }
      })

      return createElement(
        'div',
        {
          class: 'content-article',
        },
        [
          createElement(component)
        ]
      )
    }
  }

I'm trying with Vue.component but getting errors ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question