微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角度:类型上不存在属性“管道”

如何解决角度:类型上不存在属性“管道”

我试图通过单击按钮对 id 使用 modula 来过滤我的数组。在互联网上搜索我正在使用管道。但是我收到一个错误,说它“管道”不存在。我不知道为什么?仅使用 .filter() 而不使用管道是行不通的,使用它也行不通。或者我只是为了一个简单的 onclick 过滤器而走错了方向。我是 angular 的初学者。

这是我的代码

import { Component,OnInit} from '@angular/core';
import { StreamService } from '../stream.service';
import { Stream } from '../stream';
import { map } from 'rxjs/operators';


@Component({
  selector: 'app-discover',templateUrl: './discover.component.html',styleUrls: ['./discover.component.scss']
})
export class discoverComponent implements OnInit {
  streams!: Stream[];
  
  constructor(private streamService: StreamService) { 
    
  }

  ngOnInit() {
    this.getStreams();
  }

  getStreams(){
    this.streamService.getStream().subscribe((data =>{
      this.streams = data;
      console.log(this.streams);
    }))
  }


  filterIsUneven(){
  this.streams.pipe(map((streams => streams.filter(stream => stream.id % 3)))
  };

}
<div class="container">

  <div class="buttons">
    <button (click) = "filterIsUneven()"> Games </button>
    <button> Music </button>
    <button> Esports </button>
    <button> IRL </button>
    <button>Back</button>
  </div>

  <div class="streams" *ngFor="let stream of streams">
    <h3>{{stream.id}}</h3>
    <h3>{{stream.title}}</h3>
    <img src="{{stream.thumbnailUrl}}">
  </div>

</div>
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Stream } from './stream';
import { Observable} from 'rxjs';
 
@Injectable({
  providedIn: 'root'
})
export class StreamService{

constructor(private http: HttpClient) { }

getStream():Observable<Stream[]>{
  return this.http.get<Stream[]>("https://jsonplaceholder.typicode.com/albums/1/photos");
  }

getLiveStream(id:number):Observable<Stream[]> {
  const url = `https://jsonplaceholder.typicode.com/albums/1/photos?id=${id}`;
  return this.http.get<Stream[]>(url);
  }
}

解决方法

当您在 get 流中设置 this.streams 时,this.streams 的类型不是可观察的,而是 Stream[]。因此,当您调用 filterIsUneven() 时,您不能在流上使用管道方法,因为它不是可观察的。而是使用此逻辑:

filterIsUneven(){
  this.streams.filter(stream => stream.id % 3)
 };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。