Guide to integrating a native web component into Vue3

After creating the renderComponent method to display a webcomponent, I attempted to use it in my componentView.vue file.

export function renderComponent(el: Element, component: Component,props: VNodeProps,appContext: AppContext){
let vnode: VNode | undefined = createVNode(component, props)
vnode.appContext = { ...appContext }
render(vnode, el)

return ()=> {
    render(null, el)
    vnode = undefined
}
}

In my componentView.vue file, I called the addComponent function like this:

const addComponent = async(component: ComponentRegistryItem) => {
  destroyComp = renderComponent(
    containerDiv.value,
    (await import (/* @vite-ignore */ component.frontendUrl)),
    {},
    appContext);
};

This is how the template looks:

<li v-for="component in group">
        <button @click="addComponent(component)">{{ component.tagName }}</button>
        <div ref="containerDiv"></div>
      </li>

The component.frontendUrl points to a JavaScript webcomponent that extends HTMLElement (non-Vue). However, when clicking the button, nothing happens and no errors are displayed.

Is there a way to render a non-Vue component using this setup?

UPDATE:

Upon logging the imported component, I noticed this output:

Object { … }
         Symbol(Symbol.toStringTag): "Module"

Answer №1

When attempting to import using the import() method, it is important to note that the returned object contains a module and not the default export. To properly use your import, you will need to access the default import by adding .default after the import().

const loadComponent = async(component: ComponentRegistryItem) => {
  destroyComp = renderComponent(
    containerDiv.value,
    (await import(/* @vite-ignore */ component.frontendUrl)).default,
    {},
    appContext);
};

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

What could be causing my .vue component to be undefined?

I've encountered an issue while using parcel-plugin-vue to compile a basic .vue component from a file. The compilation process seems to be running smoothly with no errors, but when I try to view the page in my browser, it appears blank without any con ...

Attempting to modify information in vue.js

I have been facing an issue with overriding data in the App.vue component. It seems that every time I try to update the data in my main.js file, the component still takes the default data. I am working with webpack as well. App.vue <template> & ...

"Error: Unable to push new Grade to grades array" encountered while attempting to add a Grade to an existing array

Hi there! I'm working on a webapp that allows me to track my grades and calculate the average, but I'm having trouble adding grades to my Array using a button. Whenever I click on the Add Button, an error message pops up: TypeError: this.grades. ...

Creating a variable name dynamically using Typescript

I am looking to efficiently create multiple instances of variables and assign values in a single statement, an example of which is shown below. this.myList1[data.id] = data.id + "-" + data.desc; this.myList2[data.id] = data.id + "-" + data.desc; this.myLi ...

Increasing response buffer size in Node.js fetch for version 2.x.x

Currently in the process of implementing an API request using nodejs-fetch and I've encountered an issue. The documentation states that the maximum buffer size for fetch is 16kB, but the response I need to retrieve is 53 kB. This causes the .fetch() f ...

The child module invokes a function within the parent module and retrieves a result

Guardian XML <tr [onSumWeights]="sumWeights" > Algorithm sumWeights(): number { return // Calculate total value of all weights } Offspring @Input() onTotalWeights: () => number; canMakeChanges() { return this.onTota ...

Vue.js - Implementing multiple values (array) for a component through a property

I am currently working with vue.js components that receive their data from external sources. For example: <vue-button icon="fa-arrow-right" text="mytext"></vue-button> Initially, this setup is effective. However, I encountered a challenge wh ...

failure of text to display in d3.append

Having trouble displaying a tooltip on a rectangle in d3js. The tooltip renders, but the text doesn't show up. After researching similar issues, I discovered that I cannot directly append 'text' to a 'rect' element. I tried addin ...

Show image using Typescript model in Angular application

In my Angular (v8) project, I have a profile page where I typically display the user's photo using the following method: <img class="profile-user-img" src="./DemoController/GetPhoto?Id={{rec.Id}}" /> However, I am considering an alternative ap ...

Encountering a runtime issue with socket.io when using typescript that has been bundled by

Recently, I attempted to implement web sockets using socket.io in a Node server written in TypeScript with ExpressJS and bundled with Webpack. The server code is structured as follows: import * as Express from "express"; import * as SocketIO from "socket ...

Interference of NestJS provider classes in separate event loops causing conflicts

I'm currently facing an issue where my shared library injectables are conflicting with each other. The bootstrap file initiates this file alongside a proxy server to start local microservices import { serviceA } from '@company/serviceA' imp ...

Error TS6200 and Error TS2403: There is a conflict between the definitions of the following identifiers in this file and another file

Currently working on setting up a TypeScript node project and running into issues with two files: node_modules@types\mongoose\index.d.ts node_modules\mongoose\index.d.ts Encountering conflicts in the following identifiers when trying ...

Integrating d3.js into an Angular 2 project

Trying to incorporate the d3.js library into a MEAN application using angular2. Here are the steps I've taken: npm install d3 tsd install d3 In mypage.ts file (where I intend to show the d3.js graph) // <reference path="../../../typings/d3/d3.d ...

What is the process for accurately showcasing the attributes of a table when there is an additional intermediary table involved?

I am a beginner in programming and facing an issue with displaying the properties of a table. There is an intermediate table involved and I have set up the route like this in my nodejs: router.get('/getById/:id', (req, res) => { db.annonce ...

Guidelines for returning an object upon finishing the .map function within a promise-based function in Node.js

While working on server-side code using nodejs, I have encountered an issue with the .map() method inside a Promise. The problem is that the method returns a value before the .map() function completes its execution successfully. Here's the snippet of ...

Is it possible to determine the time format preference of the user's device in Angular? For example, whether they use a 24-hour system or a 12-hour system with AM

In Angular, is there a way to determine whether the user's time format is set to 24-hour or 12-hour system? Any help would be greatly appreciated. Thanks! ...

What is the most efficient method for managing components with dynamic templates and their corresponding data in Vue.js?

I have a question and requirement that I would like to discuss. It involves dynamically rendering templates and data using components. The scenario is as follows: The root Vue instance fetches data from the backend, and let's say the following data i ...

What is causing the child table (toggle-table) to duplicate every time a row in the parent table is clicked?

After creating a table containing GDP data for country states, I've noticed that when a user clicks on a state row, the child table displaying district GDP appears. However, there seems to be an issue with the child table as it keeps repeating. I&apos ...

Issue with VueJs router-link component

Whenever I click on a vuejs router-link element in my app.blade.php page navigation bar, I keep seeing an error in my console which is displayed below [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent comp ...

Learn how to showcase vertical text in React Native similar to a drawer

Looking for a way to display text vertically in a dynamic manner, where the length of the text can vary. Below are some examples for reference: Example 1 Example 2 <View> <View style={{}}> <View style={{ marginTop: 30, flexDire ...