简体   繁体   English

如何使用 Typescript 在 Vue 应用程序中使用 webcomponents

[英]How can I use webcomponents in a Vue application using Typescript

I am trying to use a web component in my Vue project.我正在尝试在我的 Vue 项目中使用 Web 组件。 However, it is not properly loading.但是,它没有正确加载。 I can see the element in my DOM, but just as an empty tag, not the button it should be.我可以在我的 DOM 中看到该元素,但只是作为一个空标签,而不是它应该是的按钮。 First error I got was the there was no module declared, so I declared one based on suggestion of the error message in tsconst.d.ts:我得到的第一个错误是没有声明模块,所以我根据 tsconst.d.ts 中错误消息的建议声明了一个:

declare module 'wc-button';

Now, I get a warning in the console from Vue that it is an unknown custom element and if I forget to register it under components.现在,我在 Vue 的控制台中收到一条警告,指出这是一个未知的自定义元素,如果我忘记在 components 下注册它。 But, since it is a webcomponent, I don't believe that it should be.但是,由于它是一个 webcomponent,我认为它不应该是。 I can make the message go away by adding the web component to vue.config.ignoredElements, but that is just hiding the message.我可以通过将 web 组件添加到 vue.config.ignoredElements 来使消息消失,但这只是隐藏了消息。 If I switch my code to javascript, it works fine.如果我将代码切换到 javascript,它就可以正常工作。 So I expect I need to properly declare a module for it, but I am unsure how to proceed.所以我希望我需要为它正确声明一个模块,但我不确定如何继续。 Does anyone have examples or a repo which I could compare to?有没有人有我可以比较的例子或回购?

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>

Found a workaround by importing the web component in my app.js from where Vue app is started.通过在我的 app.js 中从 Vue 应用程序启动的位置导入 Web 组件,找到了一种解决方法。 It is a bit hacky, but this allows me to avoid all the typescript issues that were blocking me.这有点hacky,但这使我能够避免所有阻碍我的打字稿问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Typescript 在 Vue 3.0 setup() 函数中使用 async/await - How can I use async/await in the Vue 3.0 setup() function using Typescript 如何在 TypeScript 中声明一个全局变量并在 Vue 中使用它 - How can I declare a global variable in TypeScript and use it across in Vue 如何获取组件类型以便我可以在 typescript 接口中使用它 - Vue 3 - How to get a component type so I can use it in a typescript interface - Vue 3 如何在带有 typescript 的 vue-cli 中使用自定义服务工作者事件 - How can I use a custom service worker events in vue-cli with typescript 如何在本地文件中导入数组以用于 Vue 3 TypeScript 项目? - How can I import an array in local file for use in a Vue 3 TypeScript project? 如何使用 v-model 和 object 使用 Vue 和 Typescript? - How do I use v-model with an object using Vue and Typescript? 如何在 Vue (Typescript) 中使用 axios? - How to use axios in Vue (Typescript)? include在typescript和Vue 3中如何使用? - How to use includes in typescript and Vue 3? 如何使用TypeScript在Vue的“数据”选项中使用JQuery元素? - How to use a JQuery element in Vue 'data' option using TypeScript? 如何使用 typescript 使用手动路由 nativescript-vue? - how to use Manual Routing nativescript-vue using typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM