gpt4 book ai didi

vue.js - 我可以在 .vue 文件中使用渲染函数吗

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:16 27 4
gpt4 key购买 nike

我希望为组件动态设置 html 标签。例如:

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

然后我可以在 html 文件中使用这样的代码:

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

现在,我想做的是使用 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
}
})

这行不通。

知道如何让它工作吗?

谢谢

最佳答案

像这样构造 test.js:

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

关于vue.js - 我可以在 .vue 文件中使用渲染函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com