gpt4 book ai didi

javascript - Vue 中的 this.$eval 是做什么的?

转载 作者:行者123 更新时间:2023-11-30 14:58:00 26 4
gpt4 key购买 nike

我正在将一个不是我写的旧脚本从 0.11 版迁移到 2.5.4 版,我收到一条警告:

Replace this.$eval('reportData | reportFilter false') with a solution using normal JavaScript Line 327: assets/js/custom-reports.js Reason: vm.$eval has been removed, as it has no real use More info: http://vuejs.org/guide/migration.html#vm-eval

我可以在代码中看到它设置了这些值:

this.$set('reportData[' + key + '].selected', !selectAll);

并在这里使用它们:

var data = this.$eval('reportData | reportFilter false');

有没有人知道这里发生了什么以及我如何重写它以一种新的方式工作?

最佳答案

看起来它正在使用名为 reportFilter 的过滤器过滤 reportData,我猜它是作为参数传递给 false 的。 Vue 2 中的过滤器工作方式不同,因此您需要将 reportFilter 移动到一个方法中,但是,如果跨多个组件使用它,您可能会想创建一个 mixin ,所以:

// Filters mixin
const Filters = {
methods:{
reportFilter(data, flag){
// reportFilter method
}
}

}

然后在使用 reportFilter 的任何组件中,您将需要使用 mixin:

new Vue({
// ...
mixins: [Filters],
// ...
})

然后您可以将您的 $eval 代码更改为:

var data = this.reportFilter(reportData, false);

这里有一个 JSFiddle,可以让您了解它是如何工作的:https://jsfiddle.net/4b8vqccs/

关于javascript - Vue 中的 this.$eval 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46952454/

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