【发布时间】:2018-08-25 04:07:06
【问题描述】:
在这个例子中,我创建的 Promise 可以正常工作。
但是来自 google api 的承诺不起作用。
它说this.youtube 未定义
index.html<script src="https://apis.google.com/js/api.js"></script>
app.component.html
<button (click)="customClick()">custom Promise</button>
<hr>
<hello name="{{ youtube }}"></hello>
<button (click)="youtubeClick()">youtube Promise</button>
app.component.ts
import { Component } from '@angular/core';
import { } from '@types/gapi.client.youtube';
import { } from '@types/gapi.auth2';
export class AppComponent {
name = 'Angular 5';
youtube = 'youtube';
egPromise(){
return new Promise<void>((resolve, reject) => {
setTimeout(function(){
resolve();
}, 1000);
});
}
customPromise(){
this.egPromise().then( () => {
this.name = 'from .then angular 5'
});
}
customClick(){
this.customPromise();
}
/****************************************************/
youtubePromise(){
gapi.client.init({
apiKey: 'key',
clientId: 'id',
scope: "https://www.googleapis.com/auth/youtube.readonly",
discoveryDocs: [
"https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest"
]
}).then(() => {
this.youtube = 'from .then youtube'
});
}
youtubeClick(){
gapi.load('client:auth2', this.youtubePromise);
}
编辑:解决方案/解释
在@vivek-doshi 的帮助下
我发现这篇文章搜索“绑定这个”
https://www.sitepoint.com/bind-javascripts-this-keyword-react/
正如帖子解释的那样
“在你的代码中 this 指的是什么并不总是很清楚,尤其是在处理回调函数时,你无法控制其调用点。”
由于我使用的是 google API,我无法控制该代码。
“这是因为在调用 promise 的回调时,函数的内部上下文发生了变化,并且 this 引用了错误的对象。”
加载库的函数使用回调函数,我什至没有想到第一个回调是问题所在。
如帖子所述,使用 ES2015 Fat Arrows 函数。
“他们总是使用封闭范围内的 this 的值。” ... “无论您使用多少层嵌套,箭头函数都将始终具有正确的上下文。”
因此,与其创建binding 和self 和that 和wherever,我认为使用=> 更干净
让我感到困惑的另一件事是 google api 要求没有参数的回调。
所以如果你尝试使用
const that = this;
gapi.load('client:auth2', this.start(that) );
它会抱怨。
但是使用 gapi.load('client:auth2', () => this.start() ); 我没有传递参数。
这对很多人来说可能很简单,但既然我在学习,我会尽量让其他也在学习的人变得简单。
谢谢大家。
【问题讨论】:
-
请将代码的相关部分贴在这里
-
抱歉,发错链接了,现在可以了
-
不过,您应该在此处发布所有相关部分以供将来的查看者使用,因为链接可能会失效。此外,非常欢迎您发布链接供人们使用它,但回答您问题所需的一切都应该在 * 上
-
关于
this的有用阅读:*.com/questions/20279484/… -
@daniel-hilgarth 可以在这里发布 Angular 5 项目吗?代码 sn-p 只有 1.2.23
标签: javascript angular promise google-api-js-client google-client-login