Angular2 fire监听节点更改会引发错误 [英] Angular2 fire listen to node changes throws an error

查看:97
本文介绍了Angular2 fire监听节点更改会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码正在运行,它应该监听节点中的更改并执行一个函数,但现在出现错误:

The code below was working and it should listen for changes in a node and execute a function but now am getting an error:

ncaught TypeError: Object(...) is not a function
at SwitchMapSubscriber.eval [as project] (changes.js:7)

因此,在我的 angular2 组件中,我有:

So, in my angular2 component I have:

private subscriptions = new Subscription();

registered: AngularFireList<any>;
constructor(private _af: AngularFireDatabase){
     this.registered = _af.list('/registered');
 }

ngOnInit() {
    this.subscriptions.add(
        this.registered.valueChanges().subscribe(
            res => {
                console.log("the value has changed");
            }
        )
    );
}

所以我在哪里出错,因为上面的错误指向:

So where am I going wrong as getting the error above which point to:

angular2fire/database/list/changes

我需要的代码是在firebase节点发生变化并登录控制台时监听

What I need my code to do is to listen to whenever there is a change in a firebase node and log to the console

订阅也已定义为:

    private subscriptions = new Subscription();

将其添加到订阅中然后我可以使用 onDestroy 生命周期并防止内存泄漏,如下所示

Adding it to the subscriptions then I can use onDestroy lifecycle and prevent memory leaks as shown below

    ngOnDestroy() {
      this.subscriptions.unsubscribe();
    }


推荐答案

这是一个受欢迎的问题这些天。请将 rxjs 5 升级到版本6.

This is a popular issue these days. Please upgrade rxjs 5 to version 6.

这是因为 <$ c中的一些重大变化$ C> AngularFire 即可。一旦升级,该功能应该按预期执行。

This happens due to some breaking changes in AngularFire. Once upgraded the function should perform as expected.

升级使用: npm install rxjs @ 6 rxjs-compat @ 6 --save Recomended

或使用以下方式回滚(不推荐):

Or rollback (Not recomended) using:

npm uninstall angularfire2
npm install angularfire2@5.0.0-rc.4
npm uninstall firebase
npm install firebase@4.8.0

参见 rxjs 官方< a href =https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md =nofollow noreferrer>迁移文档,了解操作方法和有关5和6之间变化的更多详细信息。

See the rxjs official migration doc for how-to's and more details on the changes between 5 and 6.

这篇关于Angular2 fire监听节点更改会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆