rxjs - 将某些类型的回调转换为可观察的
问题描述
我有一个这样的api:(这是一个微信小程序api。)
wx.request({
url: 'test.php',
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success (res) {
console.log(res.data)
},
fail(err) {
console.log(err)
},
complete(res) {
console.log(res.data)
}
})
但是我想像这样使用它:(我想像 observable 一样使用它。)
rxwx.request({
url: 'test.php',
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
}).subscribe(
(res) => {
console.log(res.data)
},
(err) => {
console.log(err)
},
(res) => {
console.log(res.data)
}
)
我无法wx.login
使用bindCallback
or进行转换bindNodeCallback
。请帮忙。先谢谢了
解决方案
改用 Observable 构造函数
const request=new Observable(emitter=>{
wx.request({
url: 'test.php',
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success:emitter.next
fail:emitter.error
complete:emitter.complete
})
return ()=>{ //... clearn up logic here }
}
推荐阅读
- matlab - Octave 的 MATLAB dmod 替代品是什么?
- vhdl - VHDL shift_right 数字
- postgresql - psycopg2 不插入 unicode 数据
- spring - 使用聚合函数的查询方法验证失败
- python - 使用 dict 查找矢量化 for 循环
- ios - 使用 CocoaPods 和 Swift 包管理器设置库
- reactjs - 将 Link 组件与 ListItem 和 Typescript 一起使用
- xml - Oracle, XML 插入语句
- excel - 将一个 Evaluate 语句的输出传递给另一个。出现错误 2015
- gradle - 使用 gradle Zip 任务在存档中包含单个目录