我有一个TronLink chrome 扩展,这个扩展提供了一个window.tronWeb
属性,我想在文档加载后访问这个属性。我正在尝试在mounted()
我的 Nuxt 页面组件部分执行此操作:
// ...
mounted() {
this.tronWeb = window.tronWeb;
},
// ...
但我收到undefined
。
我已经通过超时解决了这个问题:
// ...
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()
部分访问此属性?