【问题标题】:Passing google calendar data from service to component将谷歌日历数据从服务传递到组件
【发布时间】:2019-05-09 19:13:40
【问题描述】:

数组未从服务传递到组件:

service.ts页面的test()函数中,谷歌日历数据被成功读取并推送到一个名为response的数组中。所有数据日志。

lesson-summary.component.ts调用test()函数时,response数组数据不会出现在lesson-summary.component.html

感谢您的帮助!

google-calendar.service.ts

import { Injectable, Directive } from "@angular/core";
import * as moment from "moment-timezone";

declare var gapi: any;

@Injectable({
  providedIn: "root"
})
export class GoogleCalendarService {
  private response = [];

  constructor() { }

  test() {

    gapi.load("client", () => {
      gapi.client.init({
        apiKey: "API string here",
        discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"]
      }).then(() => {
        var month = moment().month();
        const firstOfMonth = moment().startOf("month").format("YYYY-MM-DD hh:mm");
        const lastOfMonth = moment().endOf("month").format("YYYY-MM-DD hh:mm");
        var firstOfMonthUTC = moment.tz(firstOfMonth, "America/Toronto").format();
        var lastOfMonthUTC = moment.tz(lastOfMonth, "America/Toronto").format();

        return gapi.client.calendar.events.list({
          calendarId: "calendar id here",
          timeMax: lastOfMonthUTC,
          timeMin: firstOfMonthUTC,
          singleEvents: true
        });
      })//end of .then

        .then((data) => {
          this.response.push.apply(this.response, data.result.items);
          console.log(data.result.items, "data.result.items");
          return this.response;
        });//end of .then

    });//end of .load
  }//end of test
}//end of export

lesson-summary.component.ts

import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import { GoogleCalendarService } from "../google-calendar.service";

declare var gapi: any;

@Component({
  selector: "app-lesson-summary",
  templateUrl: "./lesson-summary.component.html",
  styleUrls: ["./lesson-summary.component.css"]
})

export class LessonSummaryComponent implements OnInit {
  private response;

  constructor(
    private calendarService: GoogleCalendarService) {
    this.response = this.calendarService.test();
  }

  ngOnInit() {
  }

}

lesson-summary.component.html

<ul>
 <li *ngFor = "let item of response">
   {{ item.summary }}
 </li>
</ul>

【问题讨论】:

    标签: arrays angular typescript google-calendar-api google-api-js-client


    【解决方案1】:

    这是因为您以不正确的方式混合了 Promise 和同步函数,所以 test() 函数不会返回任何内容。

    尝试向您的test() 添加一个承诺:

    test() {
      return new Promise(resolve => { // <-- now test func return promise
        gapi.load("client", () => {
          gapi.client.init({
            apiKey: "API string here",
            discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"]
          }).then(() => {
            // code...
          }).then((data) => {
            // code...
            resolve(this.response); // <-- when we have the response, we are resolving the promise
          });
        });
      });
    }
    

    然后在组件中使用这个promise:

    this.calendarService.test().then(data => this.response = data);
    

    Learn more 关于 MDN 上的承诺

    【讨论】:

    • html 现在正在显示谷歌日历数据!非常感谢您的快速回复和链接以了解有关承诺的更多信息!
    猜你喜欢
    • 2017-05-06
    • 2018-06-30
    • 2019-05-26
    • 2018-07-08
    • 2017-07-21
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多