首页 > 解决方案 > 为什么修改后变量没有改变?

问题描述

我有分页课程:

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

标签: typescript

解决方案


你永远不会改变

这个.plans

你正在改变变量

this.paginationType.data


推荐阅读