没有在组件中获取RXJS行为的更新值
我正在研究此照片编辑器应用程序,其中更改在数据库中自动保存。现在,我正在尝试添加一个成功保存到DB时会显示的吐司。因此,我正在使用RXJS的ecdiveSubject
来实现这一目标。
请注意,首先将编辑器更改发送到一个名为dataprocorsservice
处理的服务,然后在另一个服务中进行API调用。同样,在dataprocessservice
中,我已经为行为设置了逻辑。
这就是DataProcessService中的代码现在的样子:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
class DataProcessService {
private toast = new BehaviorSubject<boolean>( false );
currentToastState = this.toast.asObservable()
private constructor( private apiService: ApiService ) {}
changeToastState( state: boolean ): void {
this.toast.next( state );
}
process( data ) {
// got rid of data processing for simplicity
this.apiService.postData( data )
.then( response => {
console.log('Success');
this.changeToastState( true );
})
.catch( error => {
console.log('Error: ', error);
})
}
}
这是PhotoeditorComponent:
@Component({
selector: 'editor-page',
templateUrl: './editor.component.html',
styleUrls: ['./editor.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
class PhotoEditorComponent implements OnInit {
@ViewChild('toast');
toast: ToastComponent;
isToast: boolean;
private constructor( private dataProcessService: DataProcessService ) {}
ngOnInit(): void {
this.dataProcessService.currentToastState.subscribe( state => {
this.isToast = state;
});
console.log( 'Log 1: ', this.isToast );
if ( this.isToast ) {
console.log( 'Log 2: ', this.isToast );
this.successMessage = 'Saved successfully!;
this.ref.detectChanges();
this.toast.show();
this.dataProcessService.changeToastState( false );
}
}
}
请注意,当我刷新页面时,log 1
is false
。而且,当编辑器发生更改时,它不会将其转换为true
或任何问题。 日志2
从不触发```````````'
I'm working on this photo editor app where changes are auto-saved in the db. Now I'm trying to add a toast that would display when the editor data is saved to db successfully. So I'm using RxJS's BehaviorSubject
to achieve that.
Please note that the editor changes are first sent to a service called DataProcessService
where it is processed and then an API call is made which is in another service. Also in DataProcessService
, I have set up the logic for the BehaviorSubject.
This is how the code in DataProcessService looks like now:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
class DataProcessService {
private toast = new BehaviorSubject<boolean>( false );
currentToastState = this.toast.asObservable()
private constructor( private apiService: ApiService ) {}
changeToastState( state: boolean ): void {
this.toast.next( state );
}
process( data ) {
// got rid of data processing for simplicity
this.apiService.postData( data )
.then( response => {
console.log('Success');
this.changeToastState( true );
})
.catch( error => {
console.log('Error: ', error);
})
}
}
This is the PhotoEditorComponent:
@Component({
selector: 'editor-page',
templateUrl: './editor.component.html',
styleUrls: ['./editor.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
class PhotoEditorComponent implements OnInit {
@ViewChild('toast');
toast: ToastComponent;
isToast: boolean;
private constructor( private dataProcessService: DataProcessService ) {}
ngOnInit(): void {
this.dataProcessService.currentToastState.subscribe( state => {
this.isToast = state;
});
console.log( 'Log 1: ', this.isToast );
if ( this.isToast ) {
console.log( 'Log 2: ', this.isToast );
this.successMessage = 'Saved successfully!;
this.ref.detectChanges();
this.toast.show();
this.dataProcessService.changeToastState( false );
}
}
}
Please note that Log 1
is false
when I refresh the page. And when there is a change in the editor it does not turn it to true
or whatsoever. Log 2
never fires```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这个“如果块”应该在订阅块中!
this 'if block' should be in subscribe block I guess!