Can I use the render function inside the .vue file

I want to dynamically set html tags for components. For instance:

  components: {
    test: {
      props: ['tag', 'attributes'],
      render(createElement) {
        return createElement(this.tag || 'div', {attrs: this.attributes || {}}, this.$slots.default);
      }
    }
  }

Then I can use this code in the html file:

<test tag="a" :attributes="{href:'http://www.google.com'}">a tag content</test>
<test tag="p">p tag content</test>

Now, what I want to do is to separate my components using something like Vue Loader. Basically, I want to split my different components into different files, and then import them using the main.js.

I tried something like this, but it does not work:

// components/test.js 
export default {
  components: {
    test: {
      props: ['tag', 'attributes'],
      render(createElement) {
        return createElement(this.tag || 'div', {attrs: this.attributes || {}}, this.$slots.default);
      }
    }
  }
}

// main.js
import Vue from 'vue'  // don't think this is relevant, but it there
import Test from './components/Test.js'
new Vue({
    el: '#app',
    components: {
        Test
    }
})

This does not work.

Any idea how to make it work?

thank

+4
source share
1 answer

The structure of test.js is as follows:

export default {
      props: ['tag', 'attributes'],
      render(createElement) {
        return createElement(this.tag || 'div', {attrs: this.attributes || {}}, this.$slots.default);
      }
}
+3
source

Source: https://habr.com/ru/post/1677274/


All Articles