设置一个 subject ,
然后在组件内定义一个 subscription
想要发送 事件就用 subject.next
订阅就赋值 subject.asObservable().subscribe()
直接看代码
const subject = new Subject<RcFile>();
const GallerySourceListPanel: FC<GallerySourceListPanelProps> = ({}) => {
let subscription: Subscription;
// 挂载订阅
useMount(() => {
subscription = subject
.asObservable()
.pipe(bufferTime(200))
.subscribe((file) => {
if (file.length === 0) return;
// console.info('file', file);
});
console.info('subscription', subscription);
});
// 取消挂载 取消 订阅
useUnmount(() => {
console.info('subscription-1', subscription);
if (subscription) {
console.info('取消订阅-1');
subscription.unsubscribe();
}
});
// ui代码
<div onClisk=(()=>{
// 在这里冒泡
subject.next(1234)
})/>
标签:info,订阅,console,应用,react,file,rxjs,subject,subscription
From: https://www.cnblogs.com/ifnk/p/16721516.html