Vue-Firebase:从 firebase 捕获更改的值



随着我对 vue Firebase 的学习,我发现保持 html 从 Firebase 更新其值异常困难。

如果我想在使用一次获得单个值

this.$firebaseRefs.Team_Counter.child('numOfTeam').once("value", function(snapshot){
Team_number = snapshot.val();
});

当值不更新并需要更改时,这很好。

但是我试图获取一个不断更新且位于 html 中的值

<p>{{Team_Num}}</p>

它更新一次,如果我重新加载项目,它将再次更新,但在运行时它不会更改值。

我尝试使用侦听孩子变化的函数

this.$firebaseRefs.Team_Counter.child('numOfTeam').on("child_changed", function(snapshot){
Team_number = snapshot.val();
} );

然而,我得到的结果与上面的代码相同。

正在使用

on.("child_changed" ...)

解决这个问题的正确方法是什么?还是我错过了别的东西?在读取更改的数据之前,是否需要向该值添加其他内容?

谢谢

你应该侦听值事件

值:读取并侦听对路径全部内容的更改。

this.$firebaseRefs.Team_Counter.child('numOfTeam').on('value', function(snapshot) {
// Do whatever
});

once('value')只运行 1 次,但当值更改时将触发on('value')

最新更新