Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object is a Vue component?

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

like image 239
Tuan Pham Avatar asked Mar 06 '17 11:03

Tuan Pham


3 Answers

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.

like image 62
Saurabh Avatar answered Nov 02 '22 22:11

Saurabh


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.

like image 10
chr Avatar answered Nov 02 '22 23:11

chr


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
like image 1
Abhisek Pandey Avatar answered Nov 03 '22 00:11

Abhisek Pandey