想要使用:rxjs 对流里面的元素进行操作(找出偶数,并且乘以2) import { of } from 'rxjs'; import { map, filter } from 'rxjs/operators'; const e = of([1,2,3,4,5]) e.pipe( filter(item => item % 2 === 0), map(num => num * 2) ).subscribe(v => console.log(v)) 但是打印的时候并没有任何数据,请问是代码写的有误吗?
rxjs使用到mergeMap和map,但是我完全看不懂这个是什么意思,请问是否有大佬帮解释理解一下? import { from, of } from 'rxjs'; import { mergeMap, map } from 'rxjs/operators'; // 模拟获取用户列表的函数 function getUserList() { return of([ { id: 1, name: 'User 1' }, { id: 2, name: 'User 2' }, { id: 3, name: 'User 3' }, ]); } // 模拟获取用户详细信息的函数 function getUserDetails(userId) { return of({ id: userId, details: `Details of User ${userId}` }); } // 使用 RxJS 操作符 const userList$ = getUserList(); const processedUserList$ = userList$.pipe( mergeMap(users => from(users)), mergeMap(user => getUserDetails(user.id).pipe( map(details => ({...user, details })) )) ); processedUserList$.subscribe(user => { console.log(user); });
现在有一个Service 如下: const getOSQueryBuilder = async (): Promise> => { return Database.getRepository(ConfigEntities) } class OSService { // 根据ID查询操作 static async getConfig(id: string): Promise { return new Promise((resolve) => { async function inquire(): Promise { const osQueryBuilder = await getOSQueryBuilder() const data = osQueryBuilder.findOne({ where: { id } }) resolve(data) } inquire() }) } } 但是我觉得这个static 异步方法很繁杂, 想要使用rxjs进行整改,让代码变得简洁。请问是否有大神可以指导一下呢?