Vue alert: Component resolution failed while attempting to create a global component

I am new to Vue Typescript and I have been encountering an issue while trying to create global components. I received a warning and the component did not load on the template. Here is how I attempted to create global components:

App.vue

import { createApp } from "vue"
import App from "./App.vue"
import "./registerServiceWorker"
import "./globalComponents"
import router from "./router"
import store from "./store"

createApp(App)
  .use(store)
  .use(router)
  .mount("#app")

globalComponents.ts

import { createApp } from "vue"

const app = createApp({})

// Forms
app.component("ui-input", () => import("@/components/core/ui/Input.vue"))

Input.vue

<template lang="pug">
.ui-input
  input(v-model="$attrs" v-on="$listeners")
</template>

<script lang="ts">
import { defineComponent } from "vue"

export default defineComponent({
  inheritAttrs: false
})
</script>

I would appreciate any help you can provide. Thank you in advance.

Answer №1

Vue 3 introduced a change where creating an app using createApp will result in a standalone Vue App instance. In order to add a global component, you need to add it to the app object created from createApp. Here's how you can do that:

const app = createApp({});

app.component('my-component-name', MyComponent)

app.mount("#app");

However, managing a lot of components directly in the main.ts file can become messy. To address this issue, you can create a separate file like 'globalComponents.ts' and import the components there:

Your current globalComponents.ts

import { createApp } from "vue"

const app = createApp({})

// Forms
app.component("ui-input", () => import("@/components/core/ui/Input.vue"))

The Issue

An error in the approach above is that another app instance is being created using createApp, which goes against the requirement of having all global components within the same instance.

Solution

To overcome this problem, pass the existing app instance from 'main-ts' to 'globalComponents.ts'. This way, both files share the same instance:

globalComponents.ts

import { App } from "vue";

// register components
export const registerComponents = (app: App): void => {
    app.component("ui-input", () => import("@/components/core/ui/Input.vue"));
}

You can then call the registerComponents function in 'main.ts' as follows:

main.ts

const app = createApp(App)
    .use(store)
    .use(router);

registerComponents(app);

app.mount("#app");

If you encounter the error message "[Vue warn]: Invalid VNode type: undefined (undefined)", refer to the official documentation on defining async components in Vue 3. To resolve this error, wrap your import statement with defineAsyncComponent:

globalComponents.ts

import { defineAsyncComponent } from "vue";

// register components
export const registerComponents = (app) => {
  app.component(
    "ui-input",
    defineAsyncComponent(() => import("@/components/Input.vue"))
  );
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Before installing npm packages, ensure to gracefully stop the process during pre-installation

Is there a way to stop the npm install process conditionally within a preinstall script? At the moment, I have a preinstall script named preinstall.js: if (someCondition) { process.kill(process.ppid, 'SIGKILL'); } The content of my package.js ...

Function that wraps JSX elements with the ability to infer types through generics

At the moment, this function is functioning properly function wrapElement(elem: JSX.Element) { return ({ ...props }) => React.cloneElement(elem, { ...props }) } I've been using it in this way to benefit from intelliSense for tailwind classes con ...

Utilize the "yaminncco/vue-sidebar-menu" package to customize the sidebar menu elements according to the user's role

Greetings! Currently, I am utilizing Laravel Breeze in conjunction with Inertia JS. For the sidebar, I have integrated the "yaminncco / vue-sidebar-menu package". I am curious to find out if there is a method to dynamically display menu elements ...

What steps can I take to refactor a portion of the component using React hooks?

I am trying to rewrite the life cycle methods in hooks but I am facing some issues. It seems like the component is not behaving as expected. How can I correct this? Can you provide guidance on how to properly rewrite it? useEffect(() => { updateUs ...

How can I create a custom elevation for a Vuetify v-menu?

I'm currently working with vuetify and v-menu as outlined in the official documentation here https://vuetifyjs.com/en/components/menus/ I'm struggling to figure out how to apply elevation only on the bottom left and right corners. When I add a ...

When using MERN Stack (with Typescript) on DigitalOcean, encountering an issue where HTML files are displayed instead of JS and

Upon checking the console, I encountered this https://i.sstatic.net/PWoT5.jpg The app has been developed using Ubuntu and Nginx so far with no firewall configuration yet in place. This is my first time deploying a MERN stack and utilizing DigitalOcean. ...

The system is currently unable to find the specified element

I am facing an issue trying to locate a button that is defined under a specific class using XPATH. The error message "Unable to locate element" keeps popping up. Here are the details of the class: <div class="aui-button-holder inputBtn" id="aui_3_4_0_1 ...

React is generating an error when attempting to use hooks without the appropriate class name prefix

I'm encountering an error when I use hooks without a class name prefix. Can someone help me troubleshoot this issue? import React, {Fragment,useState} from 'react'; function App (props) { const [x,updateX] = useState(1) /* throwing error ...

Improving presentation of a 5MB query outcome with Python and VueJS

I am currently working on a web application that utilizes Python (bottle) on the server and VueJS on the client side. One of my frontend components is responsible for displaying paginated results from a query that returns over 10,000 records. These records ...

Vue has detected an error during rendering: "TypeError: state.actionInfo.find is not a function"

Using vue.js's cli, I created a basic data register application. The application utilizes lookback.js api with vue cli. The application consists of three pages: show, add, and edit. While the show and add pages function correctly, issues arise when ...

Troubleshoot: Why is my Google Chrome not playing videos? Uncover the solution with a

I have created a webpage with an HTML video tag that loads dynamically from a JSON file. I am currently using Chrome 50 for this project. The response is successful, and when inspecting the page using Chrome's developer tools, I can see the video tag ...

When utilizing an Express application, what is the process for a JavaScript file within the public folder to interact with the database API located in the index.js file within

The main goal of this project is to enable the retrieval of data from a MySQL database located at localhost/phpmyadmin by simply clicking a button on the client side. The retrieved data will then be utilized to populate a table within the client interface. ...

Angular Dynamic Table Column Adaptor

Recently, I came across the adpt-strap table lite and decided to experiment with it. Here is the JSfiddle link for the project I was working on: http://jsfiddle.net/cx5gm0sa/ My main goal was to dynamically hide or show a column. In my code, I added $scop ...

JavaScript function failing to validate password

While engaging in an online game where the objective is to uncover a password by inspecting the page source or element, I encountered a puzzling line: if(el.value == ""+CodeCode+""). My assumption is that el.value represents my guess, and it indicates that ...

Using JSDoc with "T extending Component"

get_matching_components<T extends Component>(component_type_to_return: { new (doodad: Doodad): T }): T[] { return this.components.filter(component => component instanceof component_type_to_return) } In TypeScript, I created a method to retrie ...

What allows mapped types to yield primitive outputs when using {[P in keyof T]}?

Check out this innovative mapped type example that utilizes the power of keyof: type Identity<T> = { [P in keyof T]: T[P]; }; Have you ever wondered why Identity<number> results in the primitive number type, rather than an object type? Is th ...

PHP/AJAX user action history manager

Is there a library available that offers undo/redo functionality with a complete history for a web application? One possible solution could be a system using php/javascript/ajax where you can record the opposite action and variable state for each user acti ...

JavaScript: Append an ellipsis to strings longer than 50 characters

Can the ternary operator be utilized to append '...' if a string surpasses 50 characters? I attempted this approach, however it did not work as expected. {post.title.substring(0, 50) + post.title.length > 50 ? '...&ap ...

What is the best way to engage with a JavaScript/ClojureScript wrapper library for an API?

While I usually work with Python, I have recently been delving into learning Clojure/ClojureScript. To test my skills, I've set out to create a ClojureScript wrapper for Reddit's API. My main challenge lies in the asynchronous nature of Javascri ...

Advantages of choosing between the <NextLink/> and the <Button href="/somePage"/> components in the powerful Mui React UI libraries

Currently engaged in a project, I am wondering if there exists a substantial disparity in the utilization of these two components. Prior to this change, the return button was implemented as follows: <NextLink href="/settings" ...