不防用rxjs-mp,在小程序中优雅的使用rxjs。 vscode中还有rxjs的语法提示: https://www.npmjs.com/package/rxjs-mp const Rx = require('rxjs-mp'); /** * Request method * @param {string} url * @param {any} data * @param {'GET'|'POST'|'PUT'|'DELETE'} method * @param {{header?: object}} options */ function request(url, data = {}, method = "GET", { header }={}) { return new Rx.Observable(ob => { const requestTask = wx.request({ url, data, method, header: { ...(header || {}) 'Token': wx.getStorageSync('token') }, success: res => { if (res.statusCode == 200) { ob.next(res); ob.complete(); } else { ob.error(res); } }, fail: err => { console.error(err); ob.error(err); } }); return () => requestTask.abort(); }); } /** * GET method * @param {string} url * @param {{header?: object}} options */ function get(url, options) { return request(url, null, 'GET', options); } // 调用请求 const sub = get('htts://some-api-url').pipe( Rx.operators.map(res => res && res.data || {}) ).subscribe(res => { console.log(res); }, err => { console.error(err); }); // 取消请求 setTimeout(() => { sub.unsubscribe(); }, 1000);
请尽快解决 rxjs/operators 无法构建的问题?如果不能对 NPM 有良好支持,会大大影响开发人员的积极性。
2021-03-31用rxjs-mp,在小程序中优雅的使用rxjs,还有语法提示: https://www.npmjs.com/package/rxjs-mp const Rx = require('rxjs-mp'); /** * Request method * @param {string} url * @param {any} data * @param {'GET'|'POST'|'PUT'|'DELETE'} method * @param {{header?: object}} options */ function request(url, data = {}, method = "GET", { header }={}) { return new Rx.Observable(ob => { const requestTask = wx.request({ url, data, method, header: { ...(header || {}) 'Token': wx.getStorageSync('token') }, success: res => { if (res.statusCode == 200) { ob.next(res); ob.complete(); } else { ob.error(res); } }, fail: err => { console.error(err); ob.error(err); } }); return () => requestTask.abort(); }); } /** * GET method * @param {string} url * @param {{header?: object}} options */ function get(url, options) { return request(url, null, 'GET', options); } const sub = get('htts://some-api-url').pipe( Rx.operators.map(res => res && res.data || {}) ).subscribe(res => { console.log(res); }, err => { console.error(err); }); setTimeout(() => { sub.unsubscribe(); }, 1000);
rxjs/operators npm构建失败构建npm import { of } from 'rxjs'; // ok,可以使用 import { map , filter, zip } from 'rxjs/operators'; // 错误 module "pages/index/rxjs/operators" is not defined 构建的miniprogram_npm里根本就没有rxjs/operators,只有一个index.js,而index.js里又不包含operators的入口 请问如何解决?你们自己可以试试吗?
2021-03-31可以啊,用rxjs-mp: https://www.npmjs.com/package/rxjs-mp const Rx = require('rxjs-mp'); /** * Request method * @param {string} url * @param {any} data * @param {'GET'|'POST'|'PUT'|'DELETE'} method * @param {{header?: object}} options */ function request(url, data = {}, method = "GET", { header }={}) { return new Rx.Observable(ob => { const requestTask = wx.request({ url, data, method, header: { ...(header || {}) 'Token': wx.getStorageSync('token') }, success: res => { if (res.statusCode == 200) { ob.next(res); ob.complete(); } else { ob.error(res); } }, fail: err => { console.error(err); ob.error(err); } }); return () => requestTask.abort(); }); }
有没有像angular中的rxjs服务,可以完整页面件的通信?小程序可以使用rxjs之类的服务吗?
2021-03-31