【发布时间】:2018-05-18 04:09:46
【问题描述】:
我试图在ngOnit 中调用 trimObj 但抛出错误,当您使用打字稿在 angular2 中使用函数时,正确的实现是什么。打字稿新手,我将不胜感激。
app.component.ts
export class StreamComponent implements OnInit {
displayedColumns = ['ticketNum', "assetID", "severity", "riskIndex", "riskValue", "ticketOpened", "lastModifiedDate", "eventType"];
dataSource: MatTableDataSource<Element[]>;
socket = io();
constructor(private streamService: StreamService) {};
ngOnInit() {
this.socket.on('newMessage', (event) => {
console.log('New Event', event);
let data = trimObj(event);
this.dataSource.data.push(data);
this.dataSource.data = [...this.dataSource.data]
console.log("DATA",this.dataSource.filteredData);
});
});
}
trimObj(obj) => {
var neededKeys = ['ticketNum', "assetID", "severity", "riskIndex", "riskValue", "ticketOpened", "lastModifiedDate", "eventType"];
var newObj = {};
neededKeys.forEach(function (key) {
newObj[key] = obj[key];
});
return newObj;
}
}
export interface Element {
ticketNum: number;
ticketOpened: number;
eventType: string;
riskIndex: string;
riskValue: number;
severity: string;
lastModifiedDate: number;
assetID: string;
}
错误
ERROR in app.component.ts(29,17): error TS1144: '{' or ';' expected.
app.component.ts(29,20): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
app.component.ts(38,1): error TS1128: Declaration or statement expected.
【问题讨论】:
-
你的语法一团糟。在
console.log("DATA",this.dataSource.filteredData);之后有一个额外的});。trimObject是包含错误函数的方法还是字段? -
我正在尝试调用返回值的函数。
标签: javascript angular typescript