gpt4 book ai didi

vue.js - 无法访问已安装的 Nuxt (Vue) 中的窗口对象属性

转载 作者:行者123 更新时间:2023-12-04 10:14:41 24 4
gpt4 key购买 nike

我有一个 TronLink chrome 扩展程序,该扩展程序提供了 window.tronWeb 属性,我想在文档加载后访问此属性。我正在尝试在 Nuxt 页面组件的 mounted() 部分中执行此操作:

// ...
mounted() {
this.tronWeb = window.tronWeb;
},
// ...

但我收到未定义

我已经通过超时解决了这个问题:

// ...
mounted() {
let _this = this;

let attempts = 0;
setTimeout(function startGame() {
if (window.tronWeb) {
_this.tronWeb = window.tronWeb;
} else {
attempts++;
if (attempts >= 5) {
console.log(error);
} else {
setTimeout(startGame, 500);
}
}
}, 0);
},
// ...

但这看起来是一个非常奇怪的解决方案。为什么我无法直接在 mounted() 部分访问此属性?

最佳答案

试试这个:

if (process.browser) {
console.log('The window object:', window)
}

关于vue.js - 无法访问已安装的 Nuxt (Vue) 中的窗口对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61130660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com