Javascript Google API登录侦听器运行多次
我正在开发一个简单的vue模块,将事件添加到google日历中。一旦用户登录,我就有了一个侦听器,我将调用一个方法将事件添加到日历中。为了获得gapi,我使用了这个vue库。但一旦我添加事件并尝试添加另一个事件,addEvent方法将运行多次。例如,如果我添加了一次事件,那么一切看起来都很好。但如果我再次按下它,它将打开谷歌日历两次,并添加相同的事件两次Javascript Google API登录侦听器运行多次,javascript,vue.js,google-calendar-api,Javascript,Vue.js,Google Calendar Api,我正在开发一个简单的vue模块,将事件添加到google日历中。一旦用户登录,我就有了一个侦听器,我将调用一个方法将事件添加到日历中。为了获得gapi,我使用了这个vue库。但一旦我添加事件并尝试添加另一个事件,addEvent方法将运行多次。例如,如果我添加了一次事件,那么一切看起来都很好。但如果我再次按下它,它将打开谷歌日历两次,并添加相同的事件两次 谷歌日历 导出默认值{ 数据(){ 返回{ 秀:没错, 残疾人士:错,, 项目:“”, 事件:“”, gapi:null, 合作伙伴电子邮件
谷歌日历
导出默认值{
数据(){
返回{
秀:没错,
残疾人士:错,,
项目:“”,
事件:“”,
gapi:null,
合作伙伴电子邮件:“”,
listner:null,
}
}
方法:{
onSubmit(evt){
evt.preventDefault();
//警报(JSON.stringify(this.form));
//this.meetingEdit(this.form);
这是.$router.push({path:'/portal/meetingEdit/'})
},
删除会议(){
这是.$router.push({path:'/portal/meetingDelete/'})
},
addEvent(){
var startDate=新日期(this.form.MeetingTime);
var msDuration=(数字(this.form.Duration.split('):')[0])*60*60+数字(this.form.Duration.split('):')[1])*60+数字(this.form.Duration.split('):')[2])*1000;
var endDate=新日期(startDate.getTime()+msDuration);
var isoStartDate=new Date(startDate.getTime()-new Date().getTimezoneOffset()*60*1000.toISOString().split(“.”[0]”);
var isoEndDate=new Date(endDate.getTime()-(new Date().getTimezoneOffset())*60*1000.toISOString().split(“.”[0];
var invitesar=[];
var tempArr=this.form.Invitees.trim().split(',');
对于(tempArr的var电子邮件){
如果(电子邮件!==“”){
var obg={'email':email};
受邀者自动推送(obg);
}
}
var partner={'email':this.partnerEmail};
受邀者推送(合作伙伴);
此事件={
“摘要”:this.form.Topic,
“位置”:this.form.InviteLink,
“描述”:this.form.inviteDes,
“开始”:{
“日期时间”:isoStartDate,
“时区”:this.form.timeZone
},
“结束”:{
“dateTime”:IsEndDate,
“时区”:this.form.timeZone
},
“与会者”:受邀者,
“conferenceData”:null,
}
这是。$GetGapClient()。然后((gapi)=>{
//gapi.sheets.spreadsheet.get(…)
var request=gapi.client.calendar.events.insert({
'日历ID':'主要',
“conferenceDataVersion”:1,
“资源”:this.event
});
请求。执行((事件)=>{
console.log(事件);
window.open(event.htmlink,'u blank');
});
this.gapi.auth2.getAuthInstance().signOut();
this.gapi=null;
})
},
更新信号状态(bool){
如果(bool){
log('login D');
这个。addEvent();
}否则{
log('notlogin');
}
},
handleAuthClick(){
这是。$GetGapClient()。然后((gapi)=>{
this.gapi=gapi;
this.listner=this.gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus);
this.updateSigninStatus(this.gapi.auth2.getAuthInstance().isSignedIn.get());
if(this.gapi.auth2.getAuthInstance().isSignedIn.get()){
这个。addEvent();
}否则{
this.gapi.auth2.getAuthInstance().signIn();
}
})
}
}
}
但是我单击按钮addEvent()方法将运行不止一次。在函数
handleAuthClick()
中,您调用了updateSigninStatus
,其中包含对addEvent()
的调用,但在handleAuthClick()
中,您还调用了addEvent()
只在其中一个函数中调用它,这将解决您的问题
更新
您在代码沙盒中链接的代码与问题行中的代码不同
定期:[“RRULE:FREQ=DAILY;COUNT=2”]
这意味着您设置了一个重复,或者换句话说,请求在一天之后重复该事件
如果这不是您想要的,请删除此行。谢谢您的回复,我试过了。但我仍然有问题。我查看了代码沙盒的链接-它继续执行一行
recurrence:[“RRULE:FREQ=DAILY;COUNT=2”]
,该行创建了一个在1天后重复的重复事件。
<template>
<div class="row">
<div class="col-md-6 ml-5 mt-3">
<b-form>
<b-form-group id="input-group-2" label="Add to" label-for="input-2">
<b-button variant="outline-primary" @click="handleAuthClick">
<b-icon icon="calendar3" aria-hidden="true"></b-icon> Google Calendar
</b-button>
</b-form-group>
</b-form>
</div>
</div>
</template>
<script>
export default {
data(){
return {
show: true,
disabled: false,
items: '',
event: '',
gapi: null,
partnerEmail: '',
listner: null,
}
}
methods: {
onSubmit(evt) {
evt.preventDefault();
//alert(JSON.stringify(this.form));
//this.meetingEdit(this.form);
this.$router.push({ path: '/portal/meetingEdit/' })
},
deleteMeeting() {
this.$router.push({ path: '/portal/meetingDelete/' })
},
addEvent() {
var startDate = new Date(this.form.MeetingTime);
var msDuration = (Number(this.form.Duration.split(':')[0]) * 60 * 60 + Number(this.form.Duration.split(':')[1]) * 60 + Number(this.form.Duration.split(':')[2])) * 1000;
var endDate = new Date(startDate.getTime() + msDuration);
var isoStartDate = new Date(startDate.getTime() - new Date().getTimezoneOffset() * 60 * 1000).toISOString().split(".")[0];
var isoEndDate = new Date(endDate.getTime() - (new Date().getTimezoneOffset()) * 60 * 1000).toISOString().split(".")[0];
var InviteesArr = [];
var tempArr = this.form.Invitees.trim().split(',');
for (var email of tempArr) {
if (email !== "") {
var obg = { 'email': email };
InviteesArr.push(obg);
}
}
var partner = { 'email': this.partnerEmail };
InviteesArr.push(partner);
this.event = {
'summary': this.form.Topic,
'location': this.form.InviteLink,
'description': this.form.inviteDes,
'start': {
'dateTime': isoStartDate,
'timeZone': this.form.Timezone
},
'end': {
'dateTime': isoEndDate,
'timeZone': this.form.Timezone
},
'attendees': InviteesArr,
'conferenceData': null,
}
this.$getGapiClient().then((gapi) => {
// gapi.sheets.spreadsheet.get(...)
var request = gapi.client.calendar.events.insert({
'calendarId': 'primary',
'conferenceDataVersion': 1,
'resource': this.event
});
request.execute( (event)=> {
console.log(event);
window.open(event.htmlLink, '_blank');
});
this.gapi.auth2.getAuthInstance().signOut();
this.gapi = null;
})
},
updateSigninStatus(bool) {
if (bool) {
console.log('login D');
this.addEvent();
} else {
console.log('not login');
}
},
handleAuthClick() {
this.$getGapiClient().then((gapi) => {
this.gapi = gapi;
this.listner = this.gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus);
this.updateSigninStatus(this.gapi.auth2.getAuthInstance().isSignedIn.get());
if (this.gapi.auth2.getAuthInstance().isSignedIn.get()) {
this.addEvent();
} else {
this.gapi.auth2.getAuthInstance().signIn();
}
})
}
}
}
</script>