我有这样的服务:
public getUsers() {
return this.httpClient.get(environment.BASE_URL + `api/all`);
}
而在组件,我设置了用户对NGX的自举表
import { Component, OnInit, PipeTransform } from "@angular/core";
import { UserService } from "./service.service";
import { IUser } from "./users.model";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
@Component({
selector: "app-users",
templateUrl: "./users.component.html",
styleUrls: ["./users.component.css"]
})
export class UsersComponent implements OnInit {
allUsers: IUser[];
page = 1;
pageSize = 4;
collectionSize: any;
constructor(private userService: UserService) {}
ngOnInit() {
this.userService.getUsers().subscribe((data: any[]) => {
this.collectionSize = data.length;
this.allUsers = data;
});
}
get users(): IUser[] {
// tslint:disable-next-line:max-line-length
return this.allUsers.map((user, i) => ({ id: i + 1, ...user
})).slice((this.page - 1) * this.pageSize, (this.page - 1) * this.pageSize
+ this.pageSize);
}
}
我总是得到错误TypeError: Cannot read property 'map' of undefined
我试图用.pipe()
但后来我得到TypeError: Cannot read property 'pipe' of undefined
在我的DOM我拿到表中的所有数据,但只有我在控制台此错误。