[英]Javascript/Typescript 'this' scope
I am working with Ionic2 and Meteor. 我正在与Ionic2和Meteor合作。 I do however have a Javascript/Typescript issue relating to the scope of the
this
object. 但是,我确实有一个与
this
对象的范围有关的Javascript / Typescript问题。
I have read that I should use bind
when I don't have handle on this
at the appropriate level. 我已阅读 ,我应该使用
bind
的时候,我没有对手柄this
在适当的水平。
I probably don't understand the concept, because I try the following, but get an error trying to call a function. 我可能不理解这个概念,因为我尝试了以下操作,但是在尝试调用函数时遇到错误。
this.subscribe('messages', this.activeChat._id, this.senderId, () => {
this.autorun(() => {
let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
promiseMessages.then((messageData: Mongo.Collection<Message>) => {
messageData.find().forEach(function (message: Message) {
setLocalMessage.bind(message);
});
});
});
and 和
private setLocalMessage(message: Message): void {
this.localMessageCollection.insert(message);
}
I get the following error when I try build the app: 尝试构建应用程序时出现以下错误:
ERROR in ./app/pages/messages/messages.ts (72,19): error TS2304: Cannot find name 'setLocalMessage'.
UPDATE UPDATE
Thank you for the advise below. 感谢您的以下建议。
I am now using the following, and it works. 我现在正在使用以下内容,并且可以正常工作。
let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
promiseMessages.then((messageData: Mongo.Collection<Message>) => {
messageData.find().forEach((message: Message) => {
this.setLocalMessage(message);
});
});
I have read that I should use
bind
when I don't have handle on this at the appropriate level.我已经读到,如果我在适当的级别上没有处理的话,应该使用
bind
。
That's a bit outdated now, better have a look at How to access the correct `this` context inside a callback? 现在有点过时了,最好看看如何在回调中访问正确的`this`上下文? these days which also shows you how to use arrow functions.
这些天还向您展示了如何使用箭头功能。
You're getting the error message because setLocalMessage
is not a variable but still a property of this
so you have to access it as such. 你得到错误信息,因为
setLocalMessage
不是一个变量,但仍的属性this
,所以你必须访问它是这样。 There are basically three solutions in your case: 您的情况基本上有三种解决方案:
bind
messageData.find().forEach(this.setLocalMessage.bind(this));
the context argument of forEach
(assuming it's the Array
method): forEach
的上下文参数(假设它是Array
方法):
messageData.find().forEach(this.setLocalMessage, this);
another arrow function: 另一个箭头功能:
messageData.find().forEach((message: Message) => { this.setLocalMessage(message); });
There are a few things wrong here. 这里有些错误。
In ES6 (and thus TypeScript), you need to refer to instance members using explicit this, such as this.setLocalMessage
. 在ES6中(因此在TypeScript中),您需要使用显式this引用实例成员,例如
this.setLocalMessage
。 Just writing setLocalMessage
is invalid no matter where the code is. 无论代码在哪里,仅编写
setLocalMessage
都是无效的。
Inside a function
, the this
object will probably not be what you expect anyway. 在
function
内部, this
对象可能不会是您期望的。 You need to capture the this
object from outside the function and put it in a variable, like so: 您需要从函数外部捕获
this
对象并将其放入变量中,如下所示:
this.subscribe('messages', this.activeChat._id, this.senderId, () => {
this.autorun(() => {
let self = this;
let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
promiseMessages.then((messageData: Mongo.Collection<Message>) => {
messageData.find().forEach(function (message: Message) {
self.setLocalMessage(message);
});
});
});
Alternatively, you can use an arrow expression, in which this
is the same as what it is in the code around it: 或者,你可以用一个箭头表示,在
this
是相同的,因为它是在它周围的代码是什么:
this.subscribe('messages', this.activeChat._id, this.senderId, () => {
this.autorun(() => {
let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
promiseMessages.then((messageData: Mongo.Collection<Message>) => {
messageData.find().forEach(message => this.setLocalMessage(message));
});
});
});
It's not an issue of TypeScript itself. 这不是TypeScript本身的问题。 Without it, the code will just fail at runtime.
没有它,代码将在运行时失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.