How to check if an object is a Vue component?

折月煮酒 提交于 2021-02-16 09:46:32

问题


Is there a reliable way to check if an object is a Vue.js component?


回答1:


You can use instanceof, as following code:

var isVueComp = vuecomp instanceof Vue

If it isVueComp is true, it is a Vue.js componeny otherwise not.

You can also use vuecomp.prototype.constructor, which will return reference to the Object constructor function that created the instance object.

Check this fiddle.




回答2:


Like Saurabh wrote, instanceof is probably the best way to be sure. However, in case you don't want to import Vue just for this purpose, you could use this._isVue.




回答3:


The simplest way to check if a an object is a Vue component, as in 2020l, is probably the _isVue property of the component, which, in case of the given object being a Vue.js component, exists as key-value at the root of the object and returns true.

Const isVueComponent = [VUE_COMPONENT_OBJECT]._isVue


来源:https://stackoverflow.com/questions/42624620/how-to-check-if-an-object-is-a-vue-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!