Javascript =>;安格拉斯。对于传入的值,我的理解是否正确?

Javascript =>;安格拉斯。对于传入的值,我的理解是否正确?,javascript,angular,typescript,Javascript,Angular,Typescript,我正在学习RxJs 6的课程,需要问这个问题,因为这对我来说并不十分清楚 注意:这是我目前正在学习的一个类型先行教程。所以在keyup事件中,这个方法被触发 ngAfterViewInit() { const searchLessons$ = fromEvent<any>(this.input.nativeElement, 'keyup') .pipe( map(event => event.target.value), debounceTi

我正在学习RxJs 6的课程,需要问这个问题,因为这对我来说并不十分清楚

注意:这是我目前正在学习的一个类型先行教程。所以在keyup事件中,这个方法被触发

ngAfterViewInit() {
  const searchLessons$ = fromEvent<any>(this.input.nativeElement, 'keyup')
    .pipe(
      map(event => event.target.value),
      debounceTime(400),
      distinctUntilChanged(),
      // switchMap cancels prior calls.
      switchMap(search => this.loadLessons(search))
    );

  const initialLessons$ = this.loadLessons();

  this.lessons$ = concat(initialLessons$, searchLessons$);
}
ngAfterViewInit(){
const searchLessons$=fromEvent(this.input.nativeElement,'keyup')
.烟斗(
映射(event=>event.target.value),
去BounceTime(400),
distinctUntilChanged(),
//switchMap取消以前的呼叫。
switchMap(搜索=>this.loadLessons(搜索))
);
const initialLessons$=this.loadLessons();
this.lessons$=concat(initialLessons$,searchLessons$);
}
代码的意思是

  • 对于触发的所有事件,代码将从已完成的loadLessons调用中收集响应
  • 事件的值被引用为搜索
  • 然后=>将触发对loadLessons(搜索)的调用
  • 继续3:如果事件的值是一个值数组,这是否意味着对于=>调用,将为每个单独的数组值传递一个单独的loadLessons(search)调用
  • 3的继续:或者它只是在整个数组中传递

  • 以下是逐行说明:

    ngAfterViewInit() {
      const searchLessons$ = fromEvent<any>(this.input.nativeElement, 'keyup') // whenever keyup is triggered on this.input
        .pipe(
          map(event => event.target.value), // we extract input value from event target
          debounceTime(400), // we wait for last event in 400ms span
          distinctUntilChanged(), // we check that the input value did change
          switchMap(search => this.loadLessons(search)) // and with that input value changed we call this.LoadLessons and then wait for its return
        );
    
      const initialLessons$ = this.loadLessons(); // this will call initial loadLeason
    
      this.lessons$ = concat(initialLessons$, searchLessons$); // this will connect return of initial call and changes triggered by key up this is not secure for race conditions
    }
    
    ngAfterViewInit(){
    const searchLessons$=fromEvent(this.input.nativeElement,'keyup')//每当在此.input上触发keyup时
    .烟斗(
    映射(event=>event.target.value),//我们从事件目标中提取输入值
    debounceTime(400),//我们等待400ms跨度内的最后一个事件
    distinctUntilChanged(),//我们检查输入值是否更改
    switchMap(search=>this.loadLessons(search))//当输入值更改时,我们调用this.loadLessons,然后等待其返回
    );
    const initialLessons$=this.loadLessons();//这将调用initial LoadLesson
    this.lessons$=concat(initialLessons$,searchLessons$);//这将连接初始调用的返回和由键启动触发的更改。这对于竞争条件是不安全的
    }
    
    Ad1。输入上的所有按键事件

    Ad2。输入的值被引用为搜索


    Ad3。是的,它只是将数组作为参数推送

    ,而没有看到loadLessons函数,我只能假设。我还假设您使用的是concat Rxjs方法

    因此,基本上代码所做的就是,获取“课程的初始负载”,并在concatMethod上订阅它,在调用完成后,它将订阅第二个可观察的searchLessons

    每次输入搜索时,都会再次调用searchLessons,并在SearchObservable上向课程订阅添加新值


    如果给LoadSession的参数是数组,则它将取决于该方法(loadSessions)的工作方式。虽然在这种情况下可以做到,但rxjs不能做到:)

    Hey,@xesenix for Ad3。你是说整个数组作为一个调用,而不是数组中的单个值作为多个调用?是的,一次调用,假设您的
    事件.target.value
    是数组,它将作为该数组进入
    搜索
    并通过所有步骤,直到
    开关映射
    它将作为一次性调用数组放入loadLesson第一个参数。