【问题标题】:angular/rxjs observable Uncaught TypeError: Cannot read property 'isStopped' of undefinedangular/rxjs observable Uncaught TypeError: Cannot read property 'isStopped' of undefined
【发布时间】:2020-03-12 18:17:07
【问题描述】:
//example 1
const xd = new Observable(({ next }) => {
  next("3");
});

xd.subscribe(msg => console.log(msg));

//example 2
    const obs$ = new Observable();

    obs$.subscribe(mg => console.log(mg));

//example 3
 private socket: SocketIOClient.Socket;
  constructor(private url: string) {
    this.socket = io(this.url);
  }

  listen() {
    return new Observable(({ next }) => {
      this.socket.on("message", next)
    })
  }

他们都给我同样的错误,可能出了什么问题?

(未捕获的类型错误:无法读取未定义的属性“isStopped”)

【问题讨论】:

    标签: angular typescript rxjs


    【解决方案1】:

    这样使用,

    const xd = new Observable((observer) => {
      // observable execution
      observer.next("3");
    });
    
    xd.subscribe(msg =>{
       console.log(msg)
    },
    (error)=>{
      console.error(error);
    }
    );
    

    【讨论】:

    • 我认为以这种方式创建 Observable 实例存在一些问题。您可以尝试在答案中。意思是说而不是 {next}=> 在这里使用 (observer)=>{observer.next("3")};
    • 如果提供的答案有效,则将其标记为已接受,这将帮助面临相同问题的新人找到答案。干杯!!
    • 做到了,我只是无法通过电话实现它 ps:您应该更新您的答案,说在这种情况下解构不起作用,以便一见钟情
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2013-01-15
    • 2017-02-18
    • 2019-02-12
    • 2021-09-20
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多