vue-github-button icon indicating copy to clipboard operation
vue-github-button copied to clipboard

Errors in Vite project

Open 0xJacky opened this issue 2 years ago • 11 comments

Hello, I met some errors when using it in my Vite4 project(Vue3, Typescript).

截屏2023-04-15 下午12 07 17

The version of vue-github-button is 3.1.0.

Here is my code:

<script setup lang="ts">
import GithubButton from 'vue-github-button'

</script>

<template>
    <a-card style="text-align: center" :bordered="false">
        <div>
            <github-button href="https://github.com/ntkme">Follow @ntkme</github-button>
        </div>
    </a-card>
</template>

<style lang="less" scoped>

</style>

0xJacky avatar Apr 15 '23 04:04 0xJacky

This has to do with TypeScript inferring any Vue ref to be VueProxy<> by default unless its type is explicitly defined.

Since this project is in pure JS. I think a type hint would need to be added: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html

ntkme avatar Apr 15 '23 05:04 ntkme

Can you please try apply this commit https://github.com/buttons/vue-github-button/commit/1389cd2d8f3d58a25c230a55596c9aad904102f2 locally and see if it helps?

ntkme avatar Apr 15 '23 05:04 ntkme

Thank you for your quick reply. I just apply this commit https://github.com/buttons/vue-github-button/commit/1389cd2d8f3d58a25c230a55596c9aad904102f2, but it still doesn't work.

0xJacky avatar Apr 15 '23 06:04 0xJacky

I try to print the $refs._ to console, is undefined.

reset: function () {
      console.log('this.$refs._:', this.$refs._);
      console.log('this.$el.lastChild:', this.$el.lastChild);
      this.$el.replaceChild(/** @type {HTMLAnchorElement} */(this.$refs._), this.$el.lastChild)
}
image

0xJacky avatar Apr 15 '23 06:04 0xJacky

$refs._ shouldn't be undefined. - Something in component lifecycle might be broken.

E.g. The ref is defined in render, meaning it should hold reference to the <a> element even before mount.

ntkme avatar Apr 15 '23 10:04 ntkme

Yes, I think so.

0xJacky avatar Apr 15 '23 10:04 0xJacky

I open a pull request to fix this. #22

0xJacky avatar Apr 15 '23 10:04 0xJacky

I cannot reproduce this on a fresh vite project. Are you using SSR?

ntkme avatar Apr 21 '23 21:04 ntkme

I tried adding button to https://github.com/vitejs/vite-plugin-vue/tree/main/playground/ssr-vue, and also cannot reproduce your issue.

ntkme avatar Apr 21 '23 21:04 ntkme

Is it possible for you to create a repo for a minimum reproduction so that I can take a look?

ntkme avatar Apr 21 '23 21:04 ntkme

Sure, this is the minimum reproduction: https://github.com/0xJacky/vite-playground.

This problem is strange, I also cannot reproduce this on a fresh vite project.

I found that in src/layout/BaseLayout.vue, if I remove the <transition></transition>, your plugin can work normally.

Maybe check whether $refs._ is defined is the best choice to resolve this issue.

0xJacky avatar Apr 22 '23 08:04 0xJacky

3.1.2 should fix this.

ntkme avatar Jul 30 '24 03:07 ntkme