Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js: check if a component exists

Tags:

I need to do some stuff in the ready: of the root instance only when some components don't exist (they weren't declared in the HTML).

How can I check if a component exists?

like image 269
javier_domenech Avatar asked May 23 '16 11:05

javier_domenech


People also ask

How do you check if an object is a Vue component?

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.

How do I access a component Vue?

We can access the root Vue instance with $root , and the parent component with $parent . To access a child component from a parent, we can assign a ref with a name to the child component and then use this. $refs to access it.

Does Vue use component?

One important feature of Vue is the ability to use components. Components are reusable Vue instances with custom HTML elements. Components can be reused as many times as you want or used in another component, making it a child component. Data, computed, watch, and methods can be used in a Vue component.

How do you're render a component in Vuejs?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.


1 Answers

We can get the list of the loaded (global and/or local) components of a Vue instance from the instantiation options which is available through the vm.$options where the vm is the current Vue instance.

vm.$options property holds the whole options of a Vue instance. For example vm.$options.components returns an object containing the loaded components of the current Vue instace vm.

However depending on the way how a component is registered, either globally through Vue.component() or locally within a Vue instance options, the structure of the vm.$options.components would be different.

If the component is registered globally, the component will be added to vm.$options.components [[Prototype]] linkage or its __proto__.

And if the component is registered locally within the Vue instance options, the component will be added to the vm.$options.components object directly as its own property. So that we do not have to walk the proto chain to find the component.


In the following example we will see how to access the loaded components in both situations.

Notice the // [1] and // [2] comments in the code which are related to local registered components.

// the globally registered component  Vue.component('global-child', {    template: '<h2>A message from the global component</h2>'  });    var localComponent = Vue.extend({ template: '<h2>A message from the local component</h2>' });      // the root view model  new Vue({    el: 'body',    data: {      allComponents: [],      localComponents: [],      globalComponentIsLoaded: false    },    // local registered components    components: { // [1]      'local-child': localComponent    },    ready: function() {          this.localComponents = Object.keys(this.$options.components); // [2]            this.allComponents = loadedComponents.call(this);      this.globalComponentIsLoaded = componentExists.call(this, 'global-child');    }  });    function loadedComponents() {    var loaded = [];    var components = this.$options.components;    for (var key in components) {      loaded.push(key);    }    return loaded;  }    function componentExists(component) {    var components = loadedComponents.call(this);    if (components.indexOf(component) !== -1) {      return true;    }    return false;  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.24/vue.js"></script>  <h1>A message from the root Vue instance</h1>  <p>All loaded components are: {{ allComponents | json }}</p>  <p>local components are: {{ localComponents | json }}</p>  <p><code>&lt;global-child&gt;</code> component is loaded: <strong>{{ globalComponentIsLoaded }}</strong></p>  <global-child></global-child>  <local-child></local-child>
like image 66
Hashem Qolami Avatar answered Oct 21 '22 03:10

Hashem Qolami