如何在Angular2 Typescript中返回多个值

时间:2017-07-18 11:05:03

标签: angular filter pagination angular-material

在md-table中实现filter和pagination函数时,我试图在typescript中返回多个值。当我运行代码时,我得到错误检测到无法访问的代码。

 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() {}

1 个答案:

答案 0 :(得分:1)

这是因为这些方法在达到之后结束:

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: something1, something2: something2}