admin管理员组文章数量:1302333
Both h
and createVNode
are exposed from vue
.
The doc seems to suggest they are the same:
The h() function is a utility to create VNodes. It could perhaps more accurately be named createVNode().
But switch h
to createVNode
will throw:
<script lang="ts">
import { createVNode, defineComponent, h } from 'vue'
export default defineComponent({
setup() {
// works
return () => h('strong', 'Foo')
// throws
return () => createVNode('strong', 'Foo')
},
})
</script>
Both h
and createVNode
are exposed from vue
.
The doc seems to suggest they are the same:
The h() function is a utility to create VNodes. It could perhaps more accurately be named createVNode().
But switch h
to createVNode
will throw:
<script lang="ts">
import { createVNode, defineComponent, h } from 'vue'
export default defineComponent({
setup() {
// works
return () => h('strong', 'Foo')
// throws
return () => createVNode('strong', 'Foo')
},
})
</script>
Share
edited Apr 21, 2021 at 6:33
Wenfang Du
asked Apr 21, 2021 at 6:26
Wenfang DuWenfang Du
11.4k13 gold badges75 silver badges113 bronze badges
2
-
1
As the docs mention, they are not the same.
createVNode
would be a better name.. but it's still calledh
. – RonaldT Commented Apr 21, 2021 at 6:32 -
createVNode
is exposed fromvue
tho, so they are supposed to act the same? – Wenfang Du Commented Apr 21, 2021 at 6:34
3 Answers
Reset to default 6createVNode
is exposed but h
is the user friendly variant of it. If you want to call createVNode
directly you should add different type of arguments, see:
https://github./vuejs/vue-next/blob/060c5f1d0ae999cd8c8fb965e8526ffab17ac2d1/packages/runtime-core/src/vnode.ts#L326
export const createVNode = (__DEV__
? createVNodeWithArgsTransform
: _createVNode) as typeof _createVNode
function _createVNode(
type: VNodeTypes | ClassComponent | typeof NULL_DYNAMIC_COMPONENT,
props: (Data & VNodeProps) | null = null,
children: unknown = null,
patchFlag: number = 0,
dynamicProps: string[] | null = null,
isBlockNode = false
)
For h
:
props can be omitted when there are no props
You can just do:
h('strong', 'Foo')
For createVNode
, you have to do:
createVNode('strong', null, 'Foo')
//h method Actual implementation
function h(type, propsOrChildren, children) {
const l = arguments.length;
if (l === 2) {
if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
// single vnode without props
if (isVNode(propsOrChildren)) {
return createVNode(type, null, [propsOrChildren]);
}
// props without children
return createVNode(type, propsOrChildren);
}
else {
// omit props
return createVNode(type, null, propsOrChildren);
}
}
else {
if (l > 3) {
children = Array.prototype.slice.call(arguments, 2);
}
else if (l === 3 && isVNode(children)) {
children = [children];
}
return createVNode(type, propsOrChildren, children);
}
}
本文标签: javascriptAre h and createVNode the sameStack Overflow
版权声明:本文标题:javascript - Are `h` and `createVNode` the same? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741703986a2393452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论