订阅时如何使主题发出值?

let mySubject = new Subject<string>
// specify somehow that when you subscribe to mySubject, it emits 'foo'

mySubject.subscribe(value => {
  // get 'foo' here
});

最佳答案

您现在只需要使用.next将数据推送到流中

下面的代码应该工作:

const mySubject = new Subject()

mySubject.subscribe(value => {
  console.log(value)
});

mySubject.next("foo")

09-20 10:03