可观察到的在angular2中不接收下一个值 [英] Observable do not receive the next value in angular2

查看:81
本文介绍了可观察到的在angular2中不接收下一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在angular2不同组件之间传递值,我使用了注入到不同组件中的不同服务.

In order to pass value between angular2 different components, I use different services injected into different components.

在我的PagesService组件中,我定义了一个行为主体并希望传递一个值.

In my PagesService component, I define a behavior subject and want to pass a value.

import { Injectable } from '@angular/core';
import { ApiService } from '../../apiService/api.service';
import { Playlists } from '../shared/playlists.model';
import { Subject, BehaviorSubject } from 'rxjs/Rx';


@Injectable()
export class PagesService {

  private currentPlaylists: Subject<Playlists> = new BehaviorSubject<Playlists>(new Playlists());

  constructor(private service: ApiService) {

  }

  getCurrentPlaylists() {
    return this.currentPlaylists.asObservable();
  }

  setCurrentPlaylists(playlists: Playlists) {
    console.log(playlists, 'i am here');
    this.currentPlaylists.next(playlists);
  }
}

我需要将播放列表传递给名为PagesComponent的组件

I need to pass the playlists to a component called PagesComponent

 import { Component, OnInit, Input, Output, OnChanges, EventEmitter, Injectable } from '@angular/core';
    import { Http, Response } from '@angular/http';
    import { Observable } from 'rxjs/Observable';
    import { PagesService } from './shared/pages.service';
    import { Subject,BehaviorSubject } from 'rxjs/Rx';
@Component({
  selector: 'pages',
  styleUrls: ['app/pages/pages.css'],
  templateUrl: 'app/pages/pages.html',
  providers: [PagesService]
})
            export class PagesComponent {

              playlists: Playlists;

              constructor(private service: PagesService, private playlistService: PlaylistService) {
                this.playlists = new Playlists();
                this.service.getCurrentPlaylists().subscribe((playlists) => {
                  console.log(playlists, 'new playlists coming');
                  this.playlists = playlists;
                }, error => {
                  console.log(error);
                });
              }
        }

这是控制台输出:

更新播放列表后,您可以看到要传递到控制台的实际播放列表,但我没有在页面组件中收到它,

After I update the playlists, you can see the actual playlists I want to pass printed out to the console, but I do not receive it in pages component,

有什么想法吗?

谢谢

推荐答案

哪个组件正在调用setCurrentPlaylists()?

很有可能,除PagesComponent之外的某些组件正在调用此方法,而其他组件可能在其元数据中定义了providers: [PagesService].这将导致创建两个PagesService实例. PagesComponent提供(和注入)的一个实例,其他组件提供(和注入)的另一个实例.

Most likely, some component other than PagesComponent is calling this method and that other component probably has providers: [PagesService] defined in its metadata. This will result in two instances of PagesService being created – one that PagesComponent provides (and injects), and another instance that some other component provides (and injects).

这篇关于可观察到的在angular2中不接收下一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆