TS2339:类型“WritableSignal<Product[]>”上不存在属性“slice”

问题描述 投票:0回答:1

我正在尝试通过切片数据数组向我的 Angular 10.1.0 项目添加分页。但是,我收到了

TS2339: Property 'slice' does not exist on type 'WritableSignal<Data[]>'
错误。

这是我的组件的摘录:

export class Component implements OnInit {
  data = signal<Data[]>([]);
  currentPage: number = 1;

  constructor(private httoService: HttpService) {}

  ngOnInit(): void {
    this.fetchData();
  }

  fetchData(): void {
    this.httpService.getData().subscribe((data) => {
      this.data.set(data);
    });
  }

  get paginatedData() {
    const start = (this.currentPage - 1) * 100;
    const end = start + 100;
    return this.data.slice(start, end);
  }

...
}

我的http.service.ts:

export class HttpService {
  http = inject(HttpClient);
  private apiUrl = 'url'

  getProducts(): Observable<Data[]> {
    return this.http.get<Data[]>(this.apiUrl);
  }
}

我尝试在切片之前提取值:

const dataArray = Array.from(this.data().values)
但它返回 0。

angular typescript observable
1个回答
0
投票

我们需要执行信号,获取数组,然后

slice

  get paginatedData() {
    const start = (this.currentPage - 1) * 100;
    const end = start + 100;
    return this.data().slice(start, end);  // <- changed here!
  }
© www.soinside.com 2019 - 2024. All rights reserved.