我有分页课程:
export class Pagination {
public localPagination(type: IPaginationLocal): void {
this.paginationType = type;
this.fetchData();
}
public fetchData() {
this.paginationType.data = this.paginationType.data.slice(this.from, this.to);
}
}
使用:
this.plans = [1,2,3,4,5,6,7,8,9,10];
this.pagination.localPagination({
data: this.plans,
type: modePagination.LOCAL
});
console.log(this.plans);// It must be sliced
如您所见,我将变量this.plans
传递给类:this.pagination.localPagination()
:
然后,该类在方法fetchData()
中生成切片输入数据。
执行分页后,我会做
console.log(this.plans);
它应该返回切片的数组,但返回初始数组this.plans
。
答案 0 :(得分:2)
您永不改变
this.plans
您要更改变量
this.paginationType.data
答案 1 :(得分:1)
您应该返回更新的值,因为您没有修改实际的参考。
export class Pagination {
public localPagination(type: IPaginationLocal): any {
this.paginationType = type;
this.fetchData();
return this.paginationType.data;
}
public fetchData() {
this.paginationType.data = this.paginationType.data.slice(this.from, this.to);
}
}
使用方式如下
this.plans = [1,2,3,4,5,6,7,8,9,10];
this.plans = this.pagination.localPagination({
data: this.plans,
type: modePagination.LOCAL
});
console.log(this.plans);// It must be sliced
答案 2 :(得分:0)
您打印初始的东西,而不是您存储的东西:
console.log(this.pagination.paginationType.data);
Array.prototype.slice
返回一个新数组,它不会修改原始数组。