没有在组件中获取RXJS行为的更新值

发布于 2025-01-31 12:36:21 字数 2025 浏览 2 评论 0原文

我正在研究此照片编辑器应用程序,其中更改在数据库中自动保存。现在,我正在尝试添加一个成功保存到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2025-02-07 12:36:21

我想这个“如果块”应该在订阅块中!

if ( this.isToast ) {
      console.log( 'Log 2: ', this.isToast );
      this.successMessage = 'Saved successfully!;
      this.ref.detectChanges();
      this.toast.show();
      this.dataProcessService.changeToastState( false );
    }

this 'if block' should be in subscribe block I guess!

if ( this.isToast ) {
      console.log( 'Log 2: ', this.isToast );
      this.successMessage = 'Saved successfully!;
      this.ref.detectChanges();
      this.toast.show();
      this.dataProcessService.changeToastState( false );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文