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

时间:2018-10-19 16:01:44

标签: javascript vue.js google-authentication

我试图在我的Vue.js应用程序中添加一个Google登录按钮,但我找到了vue-google-oauth2插件。我安装了它,并严格按照<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script> <input name="point" id="point" type="number"> <label>Point at:</label> <button class="show">Show</button> <canvas id="myChart" width="200" height="200"></canvas>代码将其集成到我的应用程序中,方法是:

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> 方法永远不会返回true,每次我按下按钮isLoaded()(即GoogleAuthInstance时打印的插件控制台消息)时,Google Chrome控制台都会告诉我是google api is not ready。有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

使用isInit代替isLoaded,因为后者将被弃用。

答案 1 :(得分:0)

添加到 main.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");