Vue.js 中 vue-google-oauth-2 的问题



我正在尝试向我的Vue.js应用程序添加一个Google登录按钮,我找到了vue-google-oauth2插件。我安装了它并完全按照sample.html代码将其集成到我的应用程序中,如下所示:

<template>
<div>
<h1>Test</h1>
<button @click="handleClickSignIn" :disabled="!isLoaded">signIn</button>
</div>
</template>
<script>
/** 
* You should first need to place these 2 lines of code in your APP ENTRY file, e.g. src/main.js
*
* import GAuth from 'vue-google-oauth2'
* Vue.use(GAuth, {clientId: '4584XXXXXXXX-2gqknkvdjfkdfkvb8uja2k65sldsms7qo9.apps.googleusercontent.com'})
* 
*/
export default {
name: 'test',
props: [],
components: {
},
data () {
return {
isLoaded: false
}
},
computed: {
},
methods: {
handleClickSignIn(){
this.$gAuth.signIn(function (user) {
//on success do something
console.log('user', user)
}, function (error) {
//on fail do something
})
}
},
mounted(){
let that = this
let checkGauthLoad = setInterval(function(){
that.isLoaded = that.$gAuth.isLoaded()
console.log('checked', that.isLoaded)
if(that.isLoaded) clearInterval(checkGauthLoad)
}, 1000);
}
}
</script>

问题是isLoaded()方法永远不会返回true,每次我按下按钮时,Google Chrome控制台都会告诉我google api is not ready,即falseGoogleAuthInstance时打印的插件控制台消息。谁能帮我?

使用isInit而不是isLoaded,因为后者将被弃用。

添加到主.js

import GAuth from 'vue-google-oauth2'

Vue.use(GAuth, {
clientId: '....apps.googleusercontent.com',
scope: 'email',
prompt: 'consent',
fetch_basic_profile: true
})

new Vue({
...
render: (h) => h(App),
}).$mount("#app");

最新更新