更改指示计算属性中索引的值时发生内存泄漏
P粉786432579
P粉786432579 2023-09-10 15:52:53
[Vue.js讨论组]

我的计算属性有问题

correctAnswersForCurrentStage(): object {return 
    return this.correctAnswers[this.currentLevel] ?? {};
},

一些背景: this. CorrectAnswers 是一个对象,其中属性是级别,值是每个门的对象:

this.correctAnswers = {
     "1": {
        "1": 15,
        "2": 25,
        "3": 35,
        "4": 45
    },
    "2": {
        "1": 15,
        "2": 25,
        "3": 35,
        "4": 45
    },
}

所以当一个关卡完成后,我会增加关卡

this.currentLevel++

此后一切都会阻塞。我不知道是 Vue 还是 TypeScript 出现了这个问题。或者也许两者的结合? 有人知道为什么会发生这种情况吗?

我尝试关闭级别的增加,然后就没有问题了。显然我一直保持在同一水平上。但其他重置逻辑有效

跟进:

当我更改它以便它不需要使用索引时,我仍然遇到同样的问题,现在我做到了:

correctAnswersForCurrentStage(): object {
    if (this.currentLevel === 1) {
        return this.correctAnswersForLevel1;
    }
    if (this.currentLevel === 2) {
        return this.correctAnswersForLevel2;
    }
    if (this.currentLevel === 3) {
        return this.correctAnswersForLevel3;
    }
    if (this.currentLevel === 4) {
        return this.correctAnswersForLevel4;
    }
    return {};
},

P粉786432579
P粉786432579

全部回复(1)
P粉191610580

很难用当前提供的代码来判断,但我怀疑 this. CorrectAnswersForLevel1 等也是计算属性。这意味着您会得到一个循环引用,从而导致无限循环。

除此之外,代码还包含错误的引用: this. CorrectAnswers['1']this. CorrectAnswers[1] 不同,因为它会相互比较字符串和数字。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号