【发布时间】:2021-11-08 12:09:40
【问题描述】:
我正在编写 Vue js 应用程序。
在 index html 文件中,我在 head 标签中包含以下内容:
<meta name="google-signin-client_id" content="<my_client_id>.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
在编写登录组件时,我使用以下代码(这个link 帮助了我)来实现登录按钮:
template: `
...
<div id="google-signin-button"></div>
...
`
methods: {
...
onSignIn (user) {
const profile = user.getBasicProfile();
this.login(profile);
}
...
}
mounted() {
...
gapi.signin2.render('google-signin-button', {
onsuccess: this.onSignIn
})
...
},
它奏效了。现在我尝试在导航栏组件(重定向后是其他组件的一部分)中使用以下代码来实现注销,但我得到“TypeError: Cannot read properties of undefined (reading 'getAuthInstance')”:
logout: function () {
var self = this;
if(confirm("Вы действительно хотите выйти?")) {
const auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('user signed out');
self.$store.dispatch('authentication/logout')
.then(() => {
self.$router.push('/login')
})
});
}
},
我尝试了 link 的答案,但我收到 cookie 策略错误,不知道该怎么办。可能有其他方法吗?请帮忙!如果你愿意,请纠正我的语法错误。谢谢!
【问题讨论】:
标签: vue.js google-signin logout google-login google-api-js-client