whoacowboy
whoacowboy

Reputation: 7447

Determine if a component is loaded Vue 3 and load a Generic component if not

I am migrating a project from Vue 2 to Vue 3. I need to determine if a component is loaded and if it isn't, load a GenericComponent as a placeholder. In Vue 2 I was able to do it with the sample code. I have been reading the Vue 3 docs but I am unable to find anything about this.

import Vue from 'vue'
import GenericComponent from 'components/GenericComponent'

if (!Vue.options.components['custom-component']) {
  Vue.component('custom-component', GenericComponent)
}

Upvotes: 1

Views: 898

Answers (1)

Duannx
Duannx

Reputation: 8726

You can do like this:

import { getCurrentInstance } from 'vue';
const app = getCurrentInstance()
console.log('app component', app?.appContext.components['component-name'])

Note that appContext.components will only contain global components.

Upvotes: 1

Related Questions