const subject = new Subject(); subject.subscribe({ next: value => console.log(`Hello ${value}!`) }); subject.next('world'); // Output: Hello world!
const subject = new Subject(); subject.subscribe({ next: value => console.log(`Hello ${value}!`) }); subject.next('Alice'); subject.next('Bob'); subject.subscribe({ next: value => console.log(`Goodbye ${value}!`) }); subject.next('Charlie'); // Output: Hello Alice!, Hello Bob!, Hello Charlie!, Goodbye Charlie!In this example, we create a new Subject and subscribe to it. We emit values 'Alice' and 'Bob', which are received by the subscriber's `next` method and logged to the console. After that, we create a new subscriber which will log "Goodbye {value}!" to the console. Finally, we emit the value 'Charlie', which is received by both subscribers. The first subscriber logs "Hello Charlie!", and the second subscriber logs "Goodbye Charlie!". Package library: RxJS