【发布时间】:2018-05-13 19:05:14
【问题描述】:
我正在开发 Angular + Electron 应用程序,并尝试通过订阅主进程的事件广播来在服务中设置 apiBaseUrl。
由于主进程和渲染进程之间的通信是异步的,服务方法getAll() 很早就被调用,因此在apiBaseUrl 上未定义。
有没有什么地方可以在所有东西连接好之前进行 IPC 通信并在使用之前设置apiBaseUrl。
下面是我的服务代码
import { ElectronService } from 'ngx-electron';
import { IpcRenderer } from 'electron';
export class SiteService {
private apiBaseUrl:string;
private ipc: IpcRenderer;
constructor(private http: Http,
private electronService: ElectronService) {
if (this.electronService.isElectronApp) {
this.ipc = this.electronService.ipcRenderer;
this.ipc.send('request-apiBaseUrl');
this.ipc.on('apiBaseUrl-changed', (evt, url) => {
this.apiBaseUrl= url;
});
}
}
getAll(): Observable<IEditSite[]> {
let url = `${this.apiBaseUrl}site/all`;
....
}
}
【问题讨论】: