我试图在ngOnit
内调用trimObj但是抛出错误,当你使用typescript中的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.
答案 0 :(得分:2)
只需使用此
进行调用即可this.trimObj(event);
您实际上还有其他语法问题。这是您更正后的代码:
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);
const data = this.trimObj(event);
this.dataSource.data.push(data);
this.dataSource.data = [...this.dataSource.data];
console.log('DATA', this.dataSource.filteredData);
});
}
trimObj(obj): Element {
const neededKeys = [
'ticketNum',
'assetID',
'severity',
'riskIndex',
'riskValue',
'ticketOpened',
'lastModifiedDate',
'eventType'
];
const newObj: Element = <any>{};
neededKeys.forEach(function(key) {
newObj[key] = obj[key];
});
return newObj;
}
}
答案 1 :(得分:1)
您正在将trimObj
定义为类方法...您还尝试将其内联定义为箭头函数,这是一种语法错误。
实际上有一些语法错误 - 也删除了额外的});
。
有几种解决方案:
let data = this.trimObj(event);
...
trimObj(obj) {
删除=>
,因为这会导致语法错误。现在,trimObj
已正确定义为类方法,您可以使用this.trimObj()
调用它。
由于trimObj
与StreamComponent
类没有特定关系,并且是一个实用程序函数,您可以单独定义它,也可以在自己的文件中定义:
export function trimObj(obj) {
然后在StreamComponent
import { trimObj } from './utilties'
您可以trimObj()
(如果在同一文件中定义则无需导入),并使用@SpringBootApplication
public class MyApplication implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
new SpringApplicationBuilder()
.listeners(new MyApplication())
.sources(MyApplication.class)
.run(args);
}
@Override
public void onApplicationEvent(final ApplicationEnvironmentPreparedEvent event) {
if (event.getEnvironment().getActiveProfiles().length == 0) {
LOGGER.error("No active profile is set. Will terminate");
System.exit(1);
}
}
}
进行调用。