英文:
Detach listener realtime listener using modular SDK: "Property 'off' does not exist on type ' DatabaseReference'."
问题
我正在将我的实时数据库实现从Namespaced迁移到Modular。在Modular中,以下部分已经可以正常工作:
const reference = firebase.database().ref(`/projects/${uuid}`);
reference.off();
我已经重写成了:
const reference = ref(firebase, `/projects/${uuid}`);
reference.off();
根据文档,分离监听器的工作方式相同(https://firebase.google.com/docs/database/web/read-and-write#detach_listeners)。所以我期望reference.off()
在模块化版本中仍然有效。
然而,它报错了:
类型“DatabaseReference”上不存在属性“off”。
我做错了什么?
英文:
I'm migrating my Realtime database implementation from Namespaced to Modular. In Modular I have the following working correctly:
const reference = firebase.database().ref(`/projects/${uuid}`);
reference.off();
I have rewritten this to:
const reference = ref(firebase, `/projects/${uuid}`);
reference.off();
According to the docs, detaching listeners works the same (https://firebase.google.com/docs/database/web/read-and-write#detach_listeners). So I would expect reference.off()
to still work, also in the modular version.
However, it gives the error:
> Property 'off' does not exist on type ' DatabaseReference'.
What am I doing wrong?
答案1
得分: 2
根据DatabaseReference的API文档,该对象上根本没有任何方法。这就是错误消息告诉你的内容。
相反,你可以使用off()函数。请确保仔细阅读,以便将事件类型与你在任何on*()
函数中使用的事件类型匹配。
off(ref, 'your_matching_event_type');
英文:
As you can see from the API documentation for DatabaseReference, there are no methods at all on the object. That's what the error message is telling you.
Instead, you can use the off() function. Be sure to read that carefully so you can match the event type with the one you used with whatever on*()
function you used.
off(ref, 'your_matching_event_type');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论