我对数组有一个订阅,在触发事件时应重新设置订阅,但是在触发事件时,我的提交并不是因为数组为空。
我的代码如下
ngOnInit() {
this.listUnstagedFilesSubscription = this.rightPanelService.listUnstagedFilesSubject.subscribe(
(listUnstagedFiles: any) => {
this.listUnstagedFiles = listUnstagedFiles;
this.setUnstageTree(listUnstagedFiles);
});
this.rightPanelService.emitListUnstagedFilesSubject();
}
并且setUnstageTree将使用'listUnstagedFiles'填充另一个数组,该数组随后用于显示我的组件。
问题是我不知道为什么它不起作用,因为该组件始终在触发事件之前就在工作,并且如果我通过在某处调用函数ngOnInit()来强制重新加载组件,它将再次起作用>
这是调用订阅的代码
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class RightPanelService {
isView: Boolean;
isViewSubject = new Subject<Boolean>();
listUnstagedFiles: any[];
listUnstagedFilesSubject: Subject<any[]>;
listStagedFiles: any[];
listStagedFilesSubject: Subject<any[]>;
commitHash: String;
commitHashSubject: Subject<String>;
constructor() {
this.commitHashSubject = new Subject<String>();
this.isViewSubject = new Subject<Boolean>();
this.isView = true;
this.emitIsViewSubject();
this.listUnstagedFilesSubject = new Subject<any[]>();
this.listStagedFilesSubject = new Subject<any[]>();
}
emitIsViewSubject() {
this.isViewSubject.next(this.isView);
}
emitListUnstagedFilesSubject() {
this.listUnstagedFilesSubject.next(this.listUnstagedFiles);
}
emitListStagedFilesSubject() {
this.listStagedFilesSubject.next(this.listStagedFiles);
}
setView(view: boolean) {
this.isView = view;
this.emitIsViewSubject();
}
setListFileCommit(listUnstagedFiles: any[], listStagedFiles: any[]) {
this.listUnstagedFiles = listUnstagedFiles;
this.listStagedFiles = listStagedFiles;
this.emitListUnstagedFilesSubject();
this.emitListStagedFilesSubject();
}
emitCommitHashSubject() {
this.commitHashSubject.next(this.commitHash);
}
setCommitHash(hash: String) {
this.commitHash = hash;
this.emitCommitHashSubject();
}
}
答案 0 :(得分:0)
我为自己的问题找到了答案,我没有等待异步函数,也没有将变量设置为生成我的数组