TypeError:无法读取未定义的属性“管道”

时间:2019-06-10 11:49:41

标签: angular rxjs

我正在使用RxJS在Angular中编写实时搜索功能。我遇到一些错误,因为TypeError:无法读取未定义的属性“管道”。 我正在使用Angular 7,并且尝试了StackOverfloew的不同代码示例,但无法解决此问题。

app.Component.html

<input type='text' class="form-control input-txt-start" placeholder="Search Domain Name" name="domainId" (keyup)='getSearchResults(searchTerm$.next($event.target.value))'>

<ul *ngIf="results">
    <li *ngFor="let result of results | slice:0:9">
        {{ result}}
    </li>
</ul>
<p *ngIf="error">
    {{error}}
</p>

app.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators, NgForm, FormControl } from '@angular/forms';
import { SearchService } from 'src/app/services/search.service';
import { Subject } from 'rxjs';

@Component({
  ...
  providers: [SearchService]
})

export class AppComponent implements OnInit { 
  results: Object;
  searchTerm$: any = new Subject();
  error: any;

  constructor(private searchService: SearchService) { }

  ngOnit() { }

  getSearchResults(search) {
    this.searchService.search(search).subscribe((res) => {
      console.log(res);
      this.results = res;
    }, err => {
      console.log(err);
      this.error = err.message;
    });
  }
}

search.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { distinctUntilChanged } from 'rxjs/operators';
import { map } from 'rxjs/operators';
import { switchMap } from 'rxjs/operators';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})

export class SearchService {
  public httpOptions = {
    headers: new HttpHeaders({'Content-Type': 'application/json'})
  };

  baseUrl: String = `${environment.API_URL}/api/domainCharts`;
  queryUrl: String = '?search=';

  constructor( private http: HttpClient ) { }

  search(terms: Observable<string>) {
    return terms.pipe(debounceTime(500)).pipe(distinctUntilChanged()).pipe(switchMap(term => this.searchEntries(term)));
  }

  searchEntries(term) {
    return this.http.get(`${this.baseUrl}${this.queryUrl}${term}`);
  }
}

1 个答案:

答案 0 :(得分:1)

有不止一件事与您的要求不符。

首先,您要传递Subject并期望Observable。这就是为什么您会出错

  

TypeError:无法读取未定义的属性“管道”

然后,您以Subject的身份传递term(我想您要发送搜索关键字)。

根据您的情况,您不需要Subject。您可以这样做:

模板:

<input type='text' class="form-control input-txt-start" placeholder="Search Domain Name" name="domainId" (keyup)='getSearchResults($event)'>  <---- Send only $event here

组件:

getSearchResults(event) {
    this.searchService.search(event.target.value).subscribe((res) => { // <--- Get target value of event
      console.log(res);
      this.results = res;
    }, err => {
      console.log(err);
      this.error = err.message;
    });
  }
}

服务:

search(term) {  // <--- Notice that the parameter name became term instead of terms

    // You can use of() method of RxJS
    // You need to import of with "import { of } from 'rxjs';"
    // You can chain you operators in pipe() method with commas

    return of(term).pipe(
       debounceTime(500),
       distinctUntilChanged(),
       switchMap(() => this.searchEntries(term) // <--- Notice that, this function has no parameter
     );
  }