gpt4 book ai didi

angular - 如何在 Angular2 Typescript 中返回多个值

转载 作者:太空狗 更新时间:2023-10-29 17:39:16 30 4
gpt4 key购买 nike

在 md-table 中实现过滤器和分页功能时,我试图在 typescript 中返回多个值。当我运行代码时,我收到错误 Unreachable code detected。

 return Observable.merge(...displayPatientDataChanges).map(() => {
const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
const data = this._patientDatabase.data.slice();
return data.splice(startIndex, this._paginator.pageSize);

// I get the error here on the second return function
return this._patientDatabase.data.slice().filter((item: Patient) => {
let searchStr = (item.firstname + item.lastname).toLowerCase();
return searchStr.indexOf(this.filter.toLowerCase()) != -1;
});

});
}

disconnect() {}

最佳答案

那是因为方法在到达后结束:

return data.splice(startIndex, this._paginator.pageSize);

这意味着永远不会到达以下代码,因此出现错误 Unreachable code detected:

return this._patientDatabase.data.slice().filter((item: Patient) => {
let searchStr = (item.firstname + item.lastname).toLowerCase();
return searchStr.indexOf(this.filter.toLowerCase()) != -1;
});

如果你想同时返回这两个值,你需要返回一个数组或对象:

const something1 = data.splice(startIndex, this._paginator.pageSize);
const something2 = this._patientDatabase.data.slice().filter((item: Patient) => {
let searchStr = (item.firstname + item.lastname).toLowerCase();
return searchStr.indexOf(this.filter.toLowerCase()) != -1;
});
return [something1, something2] // or {something1, something2}

关于angular - 如何在 Angular2 Typescript 中返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45164982/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com