我在寻找解决方案并想出了这段代码
methods: {
handleScroll () {
console.log(window.scrollY)
}
},
created () {
window.addEventListener('scroll', this.handleScroll);
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll);
}
不幸的是,这对我不起作用。我还尝试将窗口更改为 document.body。
错误消息是 Window is not defined
原文由 hdotluna 发布,翻译遵循 CC BY-SA 4.0 许可协议
window
未定义,因为 nuxt JS 是服务器端呈现的。所以尝试使用
process.client
变量查看 链接 了解更多信息