在Angular应用中未定义gapi.client.youtube

问题描述

注意:我知道该主题看起来与其他问题相似,但有所不同

我有一个调用Google Api(gapi)的角度应用程序。

我具有调用gmail api,日历api和youtube api的功能

这里我有两个问题:

问题1 :为什么 youtube 在此处无法识别? 尽管我安装了所有类型:

enter image description here

enter image description here

Question2 :运行应用程序时,gmail api和日历api调用可以正常工作,但是youtube api调用会引发错误提示 youtube 未定义?

getMessages() {
    return gapi.client.gmail.users.messages.list({ userId: 'me',labelIds: ['INBox','UNREAD'] }).then();
}
getEvents() {
    return gapi.client.calendar.calendarList.list().then();
}

getChannels() {
    return gapi.client.youtube.channels.list({'part': 'snippet','mine': 'true'}).then();
}

由于某种原因,在getChannels()函数中出现错误

Uncaught TypeError: Cannot read property 'channels' of **undefined**

其中未定义 gapi.client.youtube 的地方。

gapi也已定义,客户端也已定义,但youtube未定义。

感谢您的帮助。如您所见,这些api的调用方式(相同的签名)没有区别,不理解为什么youtube是未定义的。

这是调试模式下的屏幕截图

enter image description here

解决方法

答案1:

检查@types/gapi.client.youtube的正确性(至少是类型定义中定义的内容):

gapi.client.channels.list(null).then()

更新 正如评论中所讨论的,我还认为类型定义是错误的,您应该在项目中扩展它(直到定义被固定为止)

答案2: 也许this answer可以帮助您

,

我相信gapi不能与node或Angular一起使用。尝试Google APIs Node.js Client

编辑:不是独立的,但是这里有一个related thread,其中包含更新和instructions