简体   繁体   English

为什么箭头功能没有传递参数?

[英]Why arrow function is not passing arguments?

I'm using angular w/ rxjs to observe user events on the interface. 我正在使用w / rxjs来观察界面上的用户事件。 However, I'm having this really simple problem with passing arguments to a method in an arrow function. 但是,在将参数传递给箭头函数中的方法时,我遇到了这个非常简单的问题。 Here is the problem: 这是问题所在:

This is not working : searchterm is not being passed to this.searchFacilities 这不起作用 :searchterm没有传递给this.searchFacilities

ngOnInit() {
 this.searchTerm$.subscribe(searchterm => this.searchFacilities);/**Not working here**/
}

searchFacilities(term: string){
  console.log(term);
  this.facilityservice.searchFacilities(term)
    .subscribe(results => this.facilityList = results);
}

But this works: 但这有效:

this.searchTerm$.subscribe(searchterm => { this.searchFacilities(searchterm); })

Clearly, I have other solutions that are pretty painless, but I really want to understand why my first approach is not working. 显然,我还有其他非常轻松的解决方案,但是我真的很想了解为什么我的第一种方法行不通。 Thanks! 谢谢!

Because the parameter is not passed directly to your function. 因为参数没有直接传递给函数。

Example from the doc : doc中的示例:

Rx.Observable.range(0, 3).subscribe(function (x) { console.log(x) });

The same example with an arrow function: 带有箭头功能的相同示例:

Rx.Observable.range(0, 3).subscribe(x => console.log(x));

Small clarification. 小澄清。 The doc says you need to pass a callback to subscribe() and that this callback will receive the value(s) emitted by the observable. 该文档说您需要将回调传递给subscribe() ,并且该回调将接收可观察对象发出的值。

We could write it like this: 我们可以这样写:

const myCallBack = (val) => console.log(val);
Observable.range(0, 3).subscribe(myCallBack);

In your case you already have a callback, this.searchFacilities . 在您的情况下,您已经有一个回调this.searchFacilities
This means you can simply write: 这意味着您可以简单地编写:

this.searchTerm$.subscribe(this.searchFacilities);

Just like you can rewrite my original example to: 就像您可以将我的原始示例重写为:

// Now `console.log` is the callback!
Observable.range(0, 3).subscribe(console.log);

In other words, the problem is not "Why arrow function is not passing arguments". 换句话说,问题不在于“为什么箭头功能未传递参数”。 The problem is that you created an arrow function whose body IS your callback, instead of just using your callback directly. 问题在于您创建了一个箭头函数,其主体您的回调,而不仅仅是直接使用回调。

The expanded version of your code would look like this: 您的代码的扩展版本如下所示:

const myCallBack = (searchterm) => {
  return this.searchFacilities;
}

As you can see, searchFacilities is neither invoked nor does it receive the searchterm param. 如您所见, searchFacilities既不被调用也不接收searchterm参数。

You could have run into the same problem with a NON-ARROW function by writing the following code (although the syntax of arrow functions does make the mistake more likely and insidious): 通过编写以下代码,您可能会遇到NON-ARROW函数所遇到的相同问题(尽管arrow函数的语法确实使该错误更可能发生并且更加隐蔽):

const myCallBack = function(searchterm) {
  return this.searchFacilities;
}

Because you're getting a reference to the searchTerm but you're not doing anything with it. 因为您正在获得对searchTerm的引用,但并未对其进行任何操作。 You could just do this.searchTerm$.subscribe(this.searchFacilities) and the term will be passed into the function. 您只需要执行this.searchTerm$.subscribe(this.searchFacilities) ,该条款就会传递到函数中。

You searchFacilities function is declared in global scope, and then is called inside of ngOnInit as a callback and its context is not anymore global scope, now this points to ngOnInit element Object. 您在全局范围内声明了searchFacilities函数,然后在ngOnInit内部将其作为回调进行调用,并且其上下文不再是全局范围,现在指向ngOnInit元素Object。 In order to work inside ngOnInit object you need to bind it and then searchFacilities be method of ngOnInit and in this way its going to work. 为了在ngOnInit对象中工作,您需要将其绑定,然后searchFacilitiesngOnInit的方法,并以此方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM