Angular2不能在另一个订阅中使用订阅的值(可观察)

时间:2022-10-17 08:22:22

I wanted to use a value from a subscribe method in another one, but it gives me undefined because it is not async. Is there a method to use those values together? I want to use this.internships another time in the following subscribe method but it becomes undefined. Thank you for helping!

我想在另一个中使用来自订阅方法的值,但是它给了我undefined,因为它不是异步的。有没有一种方法可以将这些值一起使用?我想在下面的订阅方法中再次使用this.internship,但它变得未定义。感谢您的帮助!

Code:

码:

ngOnInit(): void {
        this._internshipAssignmentService.getInternshipAssignments()
          .subscribe(internships => { this.internships = internships; <---- value which gives an object
          this.internshipsHelper = internships; console.log(this.internships)},
            error => this.msgs.push({
              severity: 'error',
              summary: 'Error',
              detail: 'Er is een onverwachte fout opgetreden.'
            }));
        this.sub = this._route.params.subscribe(
          params => {
            let id = +params['id'];
            this._internshipAssignmentService.getAllFavorites()
              .subscribe(f => {
                this.favorites = f;
                this.favorite = this.getFavoritesFromIdStudent(1);
                console.log(this.internships); <----- value which gives undefined 
                this.getFavorites(this.favorite);
              });
          }
      );
    }

1 个解决方案

#1


0  

this.internships is undefined because these calls are asynchronous, you can get more informations here.

this.internships是未定义的,因为这些调用是异步的,您可以在这里获得更多信息。

Also note that you are using multiple subscription, which is not a good practice, you should combine your observable using some operators like switchMap map,pluck, etc.

另请注意,您正在使用多个订阅,这不是一个好习惯,您应该使用某些运算符(如switchMap map,pluck等)组合您的observable。

ngOnInit(): void {
    this.sub = this._internshipAssignmentService.getInternshipAssignments().do((internships) => {
            this.internships = internships; // not needed if you just use it in next callbacks
            this.internshipsHelper = internships;
            console.log(this.internships)
        }).catch(error => {
            this.msgs.push({
                severity: 'error',
                summary: 'Error',
                detail: 'Er is een onverwachte fout opgetreden.'
            })
        }).switchMap(internships => this._route.params.pluck('id').switchMap(id => {
            return this._internshipAssignmentService.getAllFavorites().do(f => {
                this.favorites = f;
                this.favorite = this.getFavoritesFromIdStudent(1);
                this.getFavorites(this.favorite);
            })
        }))
        .subscribe();
}

#1


0  

this.internships is undefined because these calls are asynchronous, you can get more informations here.

this.internships是未定义的,因为这些调用是异步的,您可以在这里获得更多信息。

Also note that you are using multiple subscription, which is not a good practice, you should combine your observable using some operators like switchMap map,pluck, etc.

另请注意,您正在使用多个订阅,这不是一个好习惯,您应该使用某些运算符(如switchMap map,pluck等)组合您的observable。

ngOnInit(): void {
    this.sub = this._internshipAssignmentService.getInternshipAssignments().do((internships) => {
            this.internships = internships; // not needed if you just use it in next callbacks
            this.internshipsHelper = internships;
            console.log(this.internships)
        }).catch(error => {
            this.msgs.push({
                severity: 'error',
                summary: 'Error',
                detail: 'Er is een onverwachte fout opgetreden.'
            })
        }).switchMap(internships => this._route.params.pluck('id').switchMap(id => {
            return this._internshipAssignmentService.getAllFavorites().do(f => {
                this.favorites = f;
                this.favorite = this.getFavoritesFromIdStudent(1);
                this.getFavorites(this.favorite);
            })
        }))
        .subscribe();
}