TypeScript 中的无渲染 Vue 组件
Renderless Vue Component in TypeScript
我在 JavaScript 中有一个无渲染组件,我正在尝试将其转换为 TypeScript。我 运行 在 Vue.extend
-ed 组件中声明 render
函数时出错:
(method) ComponentOptions<Vue, unknown, unknown, unknown, never[], Record<never, any>>.render?(createElement: CreateElement, hack: RenderContext<Record<never, any>>): VNode
No overload matches this call.
The last overload gave the following error.
Argument of type '{ render(): void; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
Types of property 'render' are incompatible.
Type '() => void' is not assignable to type '(createElement: CreateElement, hack: RenderContext<Record<string, any>>) => VNode'.
Type 'void' is not assignable to type 'VNode'.ts(2769)
vue.d.ts(90, 3): The last overload is declared here.
这是我在 TypeScript 中尝试做的一个例子:
import Vue from 'vue'
export default Vue.extend({
render() { // error happens when I add this. I tried typing the render with :VNode
// and returning this.$scopedSlots.default({}), but error still occurs
}
})
我该如何解决这个问题?
render()
有这个签名:
render?(createElement: CreateElement, hack: RenderContext<Props>): VNode;
render()
声明注意事项:
hack
不需要在您的代码中声明
- 在无渲染函数中不需要参数声明
- return 类型是
VNode
(即单个根节点),但 Vue 实际上接受 VNode[]
作为 return(这就是 this.$scopedSlots.default()
returns)
解决方案 1: 将 return 类型指定为 VNode
,并且 return 一个包含 this.$scopedSlots.default()
的单个节点:
import Vue, { VNode, CreateElement } from 'vue'
export default Vue.extend({
render(h: CreateElement): VNode {
return h('div', this.$scopedSlots.default!({}))
}
})
解决方案 2: 在 this.$scopedSlots.default()
上使用 any
类型断言来解决类型错误:
import Vue from 'vue'
export default Vue.extend({
render() {
// The container node is expected to provide a single root,
// so it's okay to return `VNode[]` as any.
return this.$scopedSlots.default!({}) as any
}
})
我在 JavaScript 中有一个无渲染组件,我正在尝试将其转换为 TypeScript。我 运行 在 Vue.extend
-ed 组件中声明 render
函数时出错:
(method) ComponentOptions<Vue, unknown, unknown, unknown, never[], Record<never, any>>.render?(createElement: CreateElement, hack: RenderContext<Record<never, any>>): VNode
No overload matches this call.
The last overload gave the following error.
Argument of type '{ render(): void; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
Types of property 'render' are incompatible.
Type '() => void' is not assignable to type '(createElement: CreateElement, hack: RenderContext<Record<string, any>>) => VNode'.
Type 'void' is not assignable to type 'VNode'.ts(2769)
vue.d.ts(90, 3): The last overload is declared here.
这是我在 TypeScript 中尝试做的一个例子:
import Vue from 'vue'
export default Vue.extend({
render() { // error happens when I add this. I tried typing the render with :VNode
// and returning this.$scopedSlots.default({}), but error still occurs
}
})
我该如何解决这个问题?
render()
有这个签名:
render?(createElement: CreateElement, hack: RenderContext<Props>): VNode;
render()
声明注意事项:
hack
不需要在您的代码中声明- 在无渲染函数中不需要参数声明
- return 类型是
VNode
(即单个根节点),但 Vue 实际上接受VNode[]
作为 return(这就是this.$scopedSlots.default()
returns)
解决方案 1: 将 return 类型指定为 VNode
,并且 return 一个包含 this.$scopedSlots.default()
的单个节点:
import Vue, { VNode, CreateElement } from 'vue'
export default Vue.extend({
render(h: CreateElement): VNode {
return h('div', this.$scopedSlots.default!({}))
}
})
解决方案 2: 在 this.$scopedSlots.default()
上使用 any
类型断言来解决类型错误:
import Vue from 'vue'
export default Vue.extend({
render() {
// The container node is expected to provide a single root,
// so it's okay to return `VNode[]` as any.
return this.$scopedSlots.default!({}) as any
}
})