Javascript 打字机脚本,有问题;这";内部箭头函数和rxjs
我试图使用=>将rxjs的subscrive方法内部的数组推送到,但是外部数组的变量变为内部对象,因此我无法使用.pushJavascript 打字机脚本,有问题;这";内部箭头函数和rxjs,javascript,angular,typescript,rxjs,Javascript,Angular,Typescript,Rxjs,我试图使用=>将rxjs的subscrive方法内部的数组推送到,但是外部数组的变量变为内部对象,因此我无法使用.push @Component({ selector: 'devices_status-panel', templateUrl: './devices.component.html', styleUrls: ['./devices.component.scss'] }) export class DevicesComponent implements OnI
@Component({
selector: 'devices_status-panel',
templateUrl: './devices.component.html',
styleUrls: ['./devices.component.scss']
})
export class DevicesComponent implements OnInit {
public rows = Array<any>();
public columns = [
{ date: 'Fecha' },
{ deviceId: 'Equipo' },
{ error: 'Estado' },
{ statusType: 'Evento'},
{ location: 'Localidad'},
{ ip: 'Ip' },
{ version: 'Version' },
{ unencriptedMessage: 'Mensaje'}
];
constructor(private devicesData: DevicesData) {
console.log("0")
console.log(this.rows)
this.getDeviceState();
}
getDeviceState(){
this.devicesData.getStatePipe()
.subscribe(([deviceState, info]) => {
console.log("1")
console.log(this.rows)
Object.keys(deviceState).forEach((key1) => {
const thisState: DeviceState = deviceState[key1];
console.log("2")
console.log(this.rows)
Object.keys(thisState.status).forEach((key2) => {
console.log("3")
console.log(this.rows)
const status: Status = thisState.status[key2];
if (status){
const eventGroupArray: Array<Array<DeviceEvent>> = status.deviceStatus;
eventGroupArray.forEach((eventArray) => {
eventArray.forEach((event) => {
const state: StateArray = {
date: event.date,
deviceId: event.deviceId,
error: status.error,
ip: event.ip,
statusType: event.statusType,
unencriptedMessage: event.unencriptedMessage,
version: event.version,
location: null
};
if (info.info[thisState.id]){
state.location = info.info[thisState.id];
}else{
state.location = "Desconocida"
}
console.log(this.rows)
console.log(typeof this.rows)
this.rows.push(state);
});
});
}
});
});
});
console.log(this.rows)
}
}
@组件({
选择器:“设备\状态面板”,
templateUrl:'./devices.component.html',
样式URL:['./devices.component.scss']
})
导出类DeviceComponent实现OnInit{
公共行=数组();
公共列=[
{日期:'Fecha'},
{deviceId:'Equipo'},
{错误:'Estado'},
{statusType:'Evento'},
{位置:'Localidad'},
{ip:'ip'},
{version:'version'},
{未编写的消息:'Mensaje'}
];
构造函数(专用设备数据:设备数据){
控制台日志(“0”)
console.log(this.rows)
这个.getDeviceState();
}
getDeviceState(){
this.devicesData.getStatePipe()
.订阅(([deviceState,info])=>{
控制台日志(“1”)
console.log(this.rows)
Object.keys(deviceState).forEach((key1)=>{
const thisState:DeviceState=DeviceState[key1];
控制台日志(“2”)
console.log(this.rows)
Object.keys(thistate.status).forEach((key2)=>{
控制台日志(“3”)
console.log(this.rows)
const status:status=thisState.status[key2];
如果(状态){
const eventGroupArray:Array=status.deviceStatus;
eventGroupArray.forEach((eventArray)=>{
eventArray.forEach((事件)=>{
常量状态:StateArray={
日期:event.date,
deviceId:event.deviceId,
错误:status.error,
ip:event.ip,
statusType:event.statusType,
UnscriptedMessage:event.UnscriptedMessage,
版本:event.version,
位置:空
};
if(info.info[thisState.id]){
state.location=info.info[thisState.id];
}否则{
state.location=“Desconocida”
}
console.log(this.rows)
console.log(this.rows的类型)
this.rows.push(状态);
});
});
}
});
});
});
console.log(this.rows)
}
}
正如您所看到的,我在subscribe中添加了日志,就在函数调用之前,这是外部的一个数组,内部是一个对象
我试图自己解决问题,但我找不到问题所在,非常感谢您的帮助
感谢您没有向我们展示
日志
语句所揭示的内容,但是如果它们显示了不同的内容,那么发生的唯一原因是在订阅之前的日志记录时间和订阅发生的时间之间,为该行分配了内容,与使用setTimeout
的示例类似:
const foo={
示例(){
this.rows=[];
console.log(1,this.rows);
设置超时(()=>{
console.log(2,this.rows);
}, 100);
}
};
foo.example();
foo.rows={}
您尚未向我们展示日志
语句所揭示的内容,但如果它们显示了不同的内容,则发生的唯一原因是,在订阅之前的日志记录时间和订阅发生时间之间,将有某个内容分配给此.rows
,与使用setTimeout
的示例类似:
const foo={
示例(){
this.rows=[];
console.log(1,this.rows);
设置超时(()=>{
console.log(2,this.rows);
}, 100);
}
};
foo.example();
foo.rows={}代码>这可能是一个关闭问题。尝试添加
getDeviceState(){
const _that = this;
.
.
.code...
_that.rows.push(state);
}
这可能是一个关闭问题。尝试添加
getDeviceState(){
const _that = this;
.
.
.code...
_that.rows.push(state);
}