如何在Vue3中使用Google登录

huangapple go评论120阅读模式
英文:

how can i use google login in vue3

问题

尝试在Vue 3中使用vue3-google-login进行Google登录,但我一直收到以下错误信息:

vue3_google_login__WEBPACK_IMPORTED_MODULE_1__.useGoogleLogin) 不是一个函数
    在 setup 中

这是我的组件:

<template>
  <div>
    <GoogleLogin
      :clientId="clientId"
      @success="handleGoogleLoginSuccess"
      @failure="handleGoogleLoginFailure"
    />
  </div>
</template>

<script>
import { defineComponent } from 'vue';
import { useGoogleLogin } from 'vue3-google-login';

export default defineComponent({
  setup() {
    const clientId = '5873015670';

    const { signIn } = useGoogleLogin({
      clientId,
      onSuccess: handleGoogleLoginSuccess,
      onFailure: handleGoogleLoginFailure,
    });
  },
});
</script>

请问为什么我一直收到这个错误,我做错了什么?

英文:

trying to use google login in vue 3 using vue3-google-login but i keep getting

 vue3_google_login__WEBPACK_IMPORTED_MODULE_1__.useGoogleLogin) is not a function
    at setup

this is my component

  &lt;div&gt;
    &lt;GoogleLogin
      :clientId=&quot;clientId&quot;
      @success=&quot;handleGoogleLoginSuccess&quot;
      @failure=&quot;handleGoogleLoginFailure&quot;
    /&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { defineComponent } from &#39;vue&#39;;
import { useGoogleLogin } from &#39;vue3-google-login&#39;;

export default defineComponent({
  setup() {
    const clientId = &#39;5873015670&#39;;

    const { signIn } = useGoogleLogin({
      clientId,
      onSuccess: handleGoogleLoginSuccess,
      onFailure: handleGoogleLoginFailure,
    });
  },
});
&lt;/script&gt;

please why do i keep getting the error ,what i'm i doing wrong

答案1

得分: 1

首先,使用npm或yarn安装vue-google-signin-button包:

npm install vue-google-signin-button

然后,在您的Vue组件文件中,导入必要的依赖项:

import GoogleSignInButton from 'vue-google-signin-button'

将导入的组件添加到Vue组件的components部分:

components: {
  GoogleSignInButton
}

现在,您可以在模板部分中使用google-signin-button组件:

<template>
  <div>
    <google-signin-button
      :clientId="YOUR_CLIENT_ID"
      @success="onSignInSuccess"
      @failure="onSignInFailure"
    ></google-signin-button>
  </div>
</template>

在Vue组件的methods部分添加相应的方法:

methods: {
  onSignInSuccess(googleUser) {
    // 处理成功的登录
    console.log('Signed in as: ' + googleUser.getBasicProfile().getName());
  },
  onSignInFailure(error) {
    // 处理登录失败
    console.error('Sign-in failed:', error);
  }
}

有关包的更多信息,请参阅:https://www.npmjs.com/package/vue-google-signin-button

英文:

I have an idea for that try that.

First, install the vue-google-signin-button package using npm or yarn:

npm install vue-google-signin-button

Then, in your Vue component file, import the necessary dependencies:

import GoogleSignInButton from &#39;vue-google-signin-button&#39;

Add the imported component to the components section of your Vue component:

components: {
  GoogleSignInButton
},

Now, you can use the google-signin-button component in your template section:

&lt;template&gt;
  &lt;div&gt;
    &lt;google-signin-button
      :clientId=&quot;YOUR_CLIENT_ID&quot;
      @success=&quot;onSignInSuccess&quot;
      @failure=&quot;onSignInFailure&quot;
    &gt;&lt;/google-signin-button&gt;
  &lt;/div&gt;
&lt;/template&gt;

Add the corresponding methods in the Vue component's methods section:

methods: {
  onSignInSuccess(googleUser) {
    // Handle successful sign-in
    console.log(&#39;Signed in as: &#39; + googleUser.getBasicProfile().getName());
  },
  onSignInFailure(error) {
    // Handle sign-in failure
    console.error(&#39;Sign-in failed:&#39;, error);
  }
}

More info about package: https://www.npmjs.com/package/vue-google-signin-button

huangapple
  • 本文由 发表于 2023年6月13日 18:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定