isVnode 判断一个对象是否为 vnode 类型

function hasOwn() {
    return Object.prototype.hasOwnProperty.call.apply(Object.prototype.hasOwnProperty, arguments)
}

// 判断是否为 vnode 类型
function isVNode(node) {
    return node !== null && typeof node === 'object' && hasOwn(node, 'componentOptions')
}

console.log(isVNode({})) // false
console.log(isVNode(this.$createElement('div', ''))) // true

 

https://segmentfault.com/q/1010000021421280

// https://segmentfault.com/q/1010000021421280
const VNode = this.$createElement().constructor
const isVNode = obj => obj instanceof VNode

console.log(isVNode({})) // false
console.log(isVNode(this.$createElement('div', ''))) // true

 

如果你处于 render 钩子中,还可以这样定义使用。

 

posted @ 2022-11-02 10:16  贝尔塔猫  阅读(474)  评论(0)    收藏  举报