gpt4 book ai didi

javascript - 使用谷歌登录时出错 - vue "gapi is not defined"

转载 作者:行者123 更新时间:2023-12-05 00:29:01 46 4
gpt4 key购买 nike

我是 vue 的新手,一直试图在我的网页中包含一个谷歌登录按钮。但是,在我的mounted() 中有一个错误指出“gapi 未定义”。我该如何解决?我也尝试过初始化 gapi,但我不知道该放在哪里。

<template>
<div id = "signin"><div class="g-signin2">Sign in with LFA Email</div></div>
</div>

</template>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
import UserDataService from "../services/UserDataService";
export default {
data(){
return {
emailAddress:"",
signedIn:false
};
},
methods:{
onSignIn(user){
const profile = user.getBasicProfile()
this.emailAddress =profile.getEmail()
console.log(this.emailAddress)
if(this.emailAddress.indexOf("@students.org")>-1){
UserDataService.create(this.emailAddress)
this.signedIn = true


}
else{
alert("Please sign in with an LFA Email Account")
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
this.signedIn=false
}

}
},
mounted() {
gapi.signin2.render('signin', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.onSuccess,
})

}
}


</script>
<style>
@import '../../public/stylesheet.css';

</style>

最佳答案

你不能把这个标签 <script src="https://apis.google.com/js/platform.js"></script>在单个文件组件中。

将此标签放入您的public/index.html :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>t-vue</title>
</head>
<body>
<noscript>
<strong>We're sorry but t-vue doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<script src="https://apis.google.com/js/platform.js"></script>
<!-- built files will be auto injected -->
</body>
</html>


除此之外,您还需要删除属性 asyncdefer .因为当您调用 gapi mounted 中的变量该脚本尚未下载。

正确的脚本是: <script src="https://apis.google.com/js/platform.js"></script>
PS:我创建了一个适合我的最小示例,请在您的计算机上尝试:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button @click="test">Click Here</button>
</div>
<script src="https://apis.google.com/js/platform.js"></script>
<script>
let app = new Vue({
el: '#app',
methods: {
test() {
console.log('method', gapi);
},
},
mounted() {
console.log('mounted', gapi);
},
});
</script>
</body>
</html>

关于javascript - 使用谷歌登录时出错 - vue "gapi is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61100835/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com