我尝试使用计算的观看Navigator.online,但直到刷新网络才能工作?
我尝试使用计算的观看Navigator.online,但直到刷新网络才能工作?
<template><div>{{isOnline}}</div></template>
...
<scirpt>
...
computed: {
isOnline() {
return navigator.onLine;
},
},
...
</script>
I try to use computed to watch navigator.onLine but not work until I refresh the web?
<template><div>{{isOnline}}</div></template>
...
<scirpt>
...
computed: {
isOnline() {
return navigator.onLine;
},
},
...
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浏览器API不是反应性。因此,每当其价值变化时,Vue都不知道。
要收听
Navigator.Online
更改,您必须使用浏览器触发的本机事件(请参阅 docs ):注意:请小心SSR,
window
在服务器上不存在。Browsers api are not reactive. So whenever their value changes, Vue doesn't know about it.
To listen to
navigator.onLine
changes, you have to use a native event fired by the browser (see docs):Note: be careful with SSR,
window
doesn't exist on server.那样
就像@kaocash
说的
您需要的是一个观察者:
Like @Kaocash said browsers api are not reactive, so a watcher won't work
Original answer :
Well, computed property will be updated when data changes on your component,
what you need is a watcher :