What are the steps to incorporate web components into a Vue project with Typescript?

I am currently facing an issue with integrating a web component into my Vue project. Despite seeing the element in the DOM, it appears as an empty tag instead of the button it should be. The first error I encountered was the absence of a declared module. I followed the suggestion provided in the error message and declared a module in tsconst.d.ts:

declare module 'wc-button';

Now, Vue is throwing a warning in the console stating that it is an unknown custom element and needs to be registered under components. However, since it is a web component, I don't think registration is necessary. I managed to suppress the warning by adding the web component to vue.config.ignoredElements, but this approach only masks the issue. Interestingly, when I switch to using JavaScript instead of TypeScript, the component works flawlessly. This leads me to believe that I need to properly declare a module for it, but I am unsure about the next steps. Does anyone have any examples or a repository that I could refer to for guidance?

Here is the code snippet for my component:

<template>
  <div>
    <button class="btn btn-primary" @click="onClick()">
      Click!
    </button>
    <wc-button type="primary" @click="onClick()">
      Click
    </wc-button>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import wcButton from 'wc-button';

export default Vue.extend({
    data: () => ({
        buttonOutput: 'I was clicked!',
    }),
    methods: {
        onClick(): string {
            return this.buttonOutput;
        },
    },
});
</script>

Answer №1

Discovered a clever solution by incorporating the web component within my app.js file, which serves as the starting point for my Vue application. While it may seem unconventional, this workaround effectively helps me circumvent the TypeScript complications that were hindering my progress.

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

Best practice for incorporating the cq-prolyfill third-party JavaScript library into an Angular 5 application

I'm experiencing an issue with the cq-prolyfill library not functioning properly when included through a typescript import statement within an angular module. I have confirmed that it is included in my vendor bundle, but for some reason the initial se ...

Enhance your MaterialUI Button with a higher order component that accepts a component

I am currently working on implementing a Higher Order Component (HOC) for the MaterialUI Button component: import {Button as MUIButton, ButtonProps} from '@material-ui/core'; import * as React from 'react'; export const Button = (props ...

Ways to generate data following the integration of Firebase Firestore in Vue.JS

How can I display the orders data retrieved from Firebase in my browser console? See the image link below for reference. https://i.sstatic.net/HPlaC.png This is the code snippet for fetching data from Firebase and displaying it in the console: orders(){ ...

Can you explain the distinction between mutation and action?

Can you explain the rationale behind Vuex having both "actions" and "mutations?" I can see the point of components not directly modifying state, but it seems redundant to have actions trigger mutations to update the state. What sets "actions" apart from ...

When using MacOS, VScode does not support the ability to navigate to Vue3's custom components by using the command+click feature

When working on my project, I've noticed that in Windows 10, I can easily jump to the corresponding .vue file by using ctrl+click on the custom component name. However, this feature doesn't work on MacOS when I try to use command+click. I've ...

Exploring the use of a customizable decorator in Typescript for improved typing

Currently, I am in the process of creating TypeScript typings for a JavaScript library. One specific requirement is to define an optional callable decorator: @model class User {} @model() class User {} @model('User') class User {} I attempted ...

Unable to load class; unsure of origin for class labeled as 'cached'

Working on an Angular 10 project in visual studio code, I've encountered a strange issue. In the /app/_model/ folder, I have classes 'a', 'b', and 'c'. When running the application in MS Edge, I noticed that only classes ...

The 'Set-Cookie' response header failed to be recognized for a subsequent HTTP request

When a user successfully logs in, their information is stored in a cookie using the "Set-Cookie" response header. However, I am facing an issue where the cookie seems to get lost when making subsequent requests from the client. As a result, the server trea ...

using mapState to set state

My goal is to streamline the process of assigning setter methods using mapState. Currently, I am employing a workaround where I temporarily name the variable of interest (todo) as storetodo and then reference it in another computed variable called todo. m ...

Trigger a 'change password' notification using Javascript

Issue: I am currently working on developing a web application that includes a password change functionality without the use of form submission. The process I have in mind is as follows: Show a Bootstrap modal pop-up User enters new password Upon clickin ...

TypeScript combined with Vue 3: Uncaught ReferenceError - variable has not been declared

At the start of my <script>, I define a variable with type any. Later on, within the same script, I reference this variable in one of my methods. Strangely, although my IDE does not raise any complaints, a runtime error occurs in my console: Referenc ...

Bypass React Query execution when the parameter is null

I am facing an issue with a react query problem. I have a separate file containing all the queries: const useFetchApTableQuery = (date: string): UseQueryResult => { const axiosClient = axios.create() const fetchApTableQuery = async (): Promise<A ...

Invoking a function in an Angular 4 component using a <td> element

Take a look at this block of code: personListComponent.html <tr *ngFor="let person of personService.getPersons()"> <td (onShow)="getCountry(person)">{{person.name}}</td> <td>{{country}} </tr personListComponent.ts ...

VueJS form validation does not account for empty inputs in both fields

One of the challenges I'm facing is generating a form with Vue.js using the input fields below: { name: 'first_name', type: 'text', label: 'First Name', placeholder: 'First Name', ...

Creating a dynamic b-table in Vue.js with editable fields and two-way data binding

I am attempting to create a dynamic b-table with editable fields that have two-way databinding. My goal is to avoid any hardcoded values. Currently, I have the following setup: <b-table striped hover :items="filtered"> <template v-slot:cell( ...

What is the best way to determine the left and top coordinates when resizing a draggable image within a container?

I am struggling to correctly scale the image and set the left (x) and top (y) positions. Here is the code from my template: <div id="container" :style="`height: ${height}px;width: ${size}px;overflow: hidden;position: relative;`"> ...

The integration between Laravel and Vue.js seems to be causing issues with locating the CSS and JS files

I am currently working on a Laravel + Vue.js project that I need to enhance. Unfortunately, I cannot share the code due to NDA restrictions. The project consists of an API in Laravel and a front end in Laravel using Vue. After committing the project, updat ...

Limitations of MaterialUI Slider

Looking for a solution to distribute 350 points across 8 sliders, each with a range of 0-100 and 5 marks at 0, 25, 50, 75, and 100. With each step consuming or returning 25 points, the challenge lies in allowing users to adjust the points allocation withou ...

Caution: The file path in node_modules/ngx-translate-multi-http-loader/dist/multi-http-loader.js relies on the 'deepmerge' dependency

My micro-frontend angular project is using mfe (module federation). I recently updated it from angular 13 to 14 and encountered some warnings such as: node_modules\ngx-translate-multi-http-loader\dist\multi-http-loader.js depends on ' ...

Angular has its own unique way of handling regular expressions through its TypeScript

Considering the creation of an enum to store various regex patterns in my application for enhanced code reuse. For example: export enum Regex { ONE_PATTERN = /^[example]+$/g, ANOTHER_PATTERN = /^[test]{5,7}$/g } However: Encountering the TS90 ...