Rxjs vue rx:如何观察数组中对象的值不再变化?

Rxjs vue rx:如何观察数组中对象的值不再变化?,rxjs,vue-rx,Rxjs,Vue Rx,我是vue rx和rxjs的新手,但是当我看到一些rx的演示时,我对此非常感兴趣。所以我想在我的项目中使用它,当属性num不再改变时,我会发布一个请求 "vue-rx": "^6.1.0", "rxjs": "^6.4.0", "vue": "^2.5.17", 以下是我的解决方案: 使用$watchAsObservable查看sendCalledTime的更改,然后使用mergeMap发布请求 变量sendCalledTimes是一个数字,当调用sendCalledTimes++函数时,该变

我是
vue rx
rxjs
的新手,但是当我看到一些
rx
的演示时,我对此非常感兴趣。所以我想在我的项目中使用它,当属性
num
不再改变时,我会发布一个请求

"vue-rx": "^6.1.0",
"rxjs": "^6.4.0",
"vue": "^2.5.17",
以下是我的解决方案:

使用
$watchAsObservable
查看
sendCalledTime
的更改,然后使用
mergeMap
发布请求

变量
sendCalledTimes
是一个
数字
,当调用
sendCalledTimes++
函数时,该变量将
sendCalledTimes++
,并且在发布请求后,将其重置为
sendCalledTimes=0

因此,
$watchAsObservable('sendCalledTimes')
(vue rx)将每三秒执行一次,并将减少项目中的请求时间。但我认为它仍然不好,因为它就像一个计时器,不能观察数组中每个对象的天气变化。好的例子应该是这样的

另外,由于
vue rx
在github上没有太多的示例,因此我需要帮助解决为这种情况创建良好订阅的问题

我尝试过这个,但失败了:

data() {
  return {
    sendCalledTimes: 0,
    giftArr: []
  }
},
created() {
  this.$watchAsObservable('sendCalledTimes').pipe(
      pluck('newValue'),
      filter(val => val > 0),
      debounceTime(3000),
      // if `sendCalledTimes` is the same number as previous
      // will not execute follows
      // distinctUntilChanged(), 
      mergeMap(
        (val) => this.requestSendGift()
      ),
    ).subscribe(
      (val) => {  }
  )
},
methods: {
  send (obj) {
    let pushFlag = true
    for (const gift in this.giftArr) {
      if (gift.id === obj.id) {
        gift.num++
        pushFlag = false
        break
      }
    }

    if (pushFlag) {
      this.giftArr.push(obj)
    }
    // observable
    this.sendCalledTimes++
  },
  async requestSendGift () {
    for (const gift in this.giftArr) {
      // example for post a request to store each gift
      await axios({
        data: gift,
        type: 'post',
        url: '...'
      }).then(res => { ... })
    }
    // reset `this.sendCalledTimes`
    this.sendCalledTimes = 0
  }
}

如果有人能帮我解决这个问题,我将不胜感激。

从你的问题来看,你到底想做什么有点不清楚,但我已经根据我认为是你的意图创建了一个例子

我做了一些假设:

  • 你有一个“礼物”数组,它代表所有将永远存在的礼物
  • 您希望对该阵列进行更新
  • 每次对数组进行更新时,您都希望看到以可观察事件形式进行的更新
使用主题 我想你想要的是一个

使其在更新时发出 您可以将其设置为每次递增
num
或添加新礼物时发射

const gift$ = new Subject();
总之,它可能看起来像这样:

function addGift(gift) {
  gifts.push(gift);
  gift$.next(gift);
}

function incrementGift(gift) {
  gift.num++;
  gift$.next(gift);
}

我已经更新了我的问题,很抱歉没有解释清楚。我所要做的就是观察每个礼物的变化,这样我就可以在一次内将它们发布到axios,但我所拥有的只是一个变量,另一次将是相同的数字,因此,我无法在我的代码中使用
distinctUntilChanged
。非常感谢,仍在寻求帮助和建议。感谢更新问题。我建议大家回顾一下SO提出问题的推荐方式。这里有很多信息,要解析出你的实际问题仍然有点困难。即使有了你的更新,我可能仍然会以同样的方式回答这个问题。
const gift$ = new Subject();
function addGift(gift) {
  gifts.push(gift);
  gift$.next(gift);
}

function incrementGift(gift) {
  gift.num++;
  gift$.next(gift);
}
import { Subject } from 'rxjs';

const gift$ = new Subject();
const gifts = [{ id: 0, name: 'giftA', num: 0 }, { id: 1, name: 'giftB', num: 0 }];

function addGift(gift) {
  gifts.push(gift);
  gift$.next(gift);
}

function incrementGift(gift) {
  gift.num++;
  gift$.next(gift);
}

function sendGift(newGift) {
  const currentGift = gifts.find(g => g.id === newGift.id);
  currentGift ? incrementGift(currentGift) : addGift(newGift);
}

gift$.subscribe(update => {
  console.log(gifts);
  console.log(update);
});

// You should see an initial logging of 'gifts' and update will be 'undefined' at first. Then you'll see a log for every 'sendGift'.

sendGift({ id: 0 });
sendGift({ id: 3, name: 'giftC', num: 0 });