我可以在.vue文件中使用渲染功能吗

Mos*_*she 5 vue.js vue-component vuejs2

我正在寻找动态设置组件的html标签。例如:

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

然后,我可以在html文件中使用如下代码:

<test tag="a" :attributes="{href:'http://www.google.com'}">a tag content</test>
<test tag="p">p tag content</test>
Run Code Online (Sandbox Code Playgroud)

现在,我想做的就是使用Vue Loader之类的东西拆分组件。基本上,我想将不同的组件拆分成不同的文件,然后使用main.js文件导入它们。

我尝试过类似的方法,但是不起作用:

// 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's there
import Test from './components/Test.js'
new Vue({
    el: '#app',
    components: {
        Test
    }
})
Run Code Online (Sandbox Code Playgroud)

这是行不通的。

知道如何使其工作吗?

谢谢

Ber*_*ert 4

像这样构造 test.js:

export default {
      props: ['tag', 'attributes'],
      render(createElement) {
        return createElement(this.tag || 'div', {attrs: this.attributes || {}}, this.$slots.default);
      }
}
Run Code Online (Sandbox Code Playgroud)