我正在尝试在 Vue 3 中呈现模板。模板包含一个组件,该组件在实例上本地注册。
import template from './template'
import RenderlessPagination from "./RenderlessPagination";
import {h, resolveComponent} from 'vue'
export default {
name: 'Pagination',
components: {RenderlessPagination},
provide() {
return {
Page: () => this.value,
perPage: () => this.perPage,
records: () => this.records
}
},
render() {
const RLPagination = resolveComponent('renderless-pagination');
return h(RLPagination, {
slots: {
default: function (props) {
return props.override ? h(
props.override,
{
attrs: {props}
}
) : template(props)(h)
}
}
})
},
props: {
value: {
type: Number,
required: true,
validator(val) {
return val > 0;
}
},
records: {
type: Number,
required: true
},
perPage: {
type: Number,
default: 25
},
options: {
type: Object
}
},
data: function () {
return {
aProps: {
role: "button"
}
}
}
}
这会导致以下运行时警告:
resolveComponent can only be used in render() or setup()
此外,该renderless-pagination
组件没有被编译,而是按原样添加到 HTML 中:
<renderless-pagination slots=[Object Object]></renderless-pagination>
正如你所看到的,我resolveComponent
在函数中使用render
,这让我想知道它为什么会抱怨,以及为什么renderless-pagination
组件没有被编译。
我检查了resolveComponent
调用返回的值,它只返回组件的名称,即renderless-pagination
.
我在这里想念什么?
编辑:
在@skirtle 的评论之后,我externals
使用webpack
. 但是,我仍然收到相同的错误:
resolveComponent can only be used in render() or setup()
相关webpack
配置:
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'vue-pagination-2.min.js',
library:'Pagination',
libraryTarget: "commonjs"
},
externals: {
vue: {
root: 'Vue',
commonjs: 'vue',
commonjs2: 'vue',
amd: 'vue'
}
}