【问题标题】:Vue-Firebase: Catching a changed value from firebaseVue-Firebase:从 firebase 捕获更改的值
【发布时间】:2018-11-26 12:33:04
【问题描述】:

随着我对使用 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" ...)

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

谢谢

【问题讨论】:

    标签: javascript firebase firebase-realtime-database vue.js


    【解决方案1】:

    你应该听value event

    value:读取并监听路径全部内容的变化。

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

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

    【讨论】:

    • 啊,我明白这只是使用一次和一次之间的区别。现在觉得有点傻。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2017-08-18
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多