【问题标题】:Using props in computed Vue.js在计算的 Vue.js 中使用道具
【发布时间】:2020-03-19 11:09:37
【问题描述】:

我有这样的计算属性:

  display() {
     return this.labs.map(function(x, i) {
        return [x, this.plotDt[i]];
      });
    }

它接收数据作为道具:

  props: ["plotDt", "labs"],

哪些是相同长度的数组(我输入两个数组:[a,b,c][1,2,3] 并期望得到映射数组:[[a,1],[b,2],[c,3]]

但不知何故,它不起作用,当我签入 VueTools 时,我收到消息:"error during evaluation" 我真的不知道在这种情况下会出现什么问题。

【问题讨论】:

标签: javascript vue.js vuejs2


【解决方案1】:

可能的解决方法是:

 display() {
     const vm = this;
     return this.labs.map(function(x, i) {
        return [x, vm.plotDt[i]];
      });
    }

您也可能需要使用方法而不是计算。介意分享代码笔吗?

【讨论】:

  • 你有处理这个案例的资源吗?还是你只是当场想到的?
  • 这个in函数可能与vue实例无关,所以只是我的想法。
【解决方案2】:

this 在函数内部没有作用域,除非使用箭头函数或将thismap 绑定

这个问题可以像这样解决

display() {
 return this.labs.map((x, i)=> {
    return [x, this.plotDt[i]];
 });
}

display() {
 return this.labs.map(function(x, i) {
    return [x, this.plotDt[i]];
  },this);
}

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2023-04-05
    • 2023-02-17
    • 2018-12-15
    • 2021-05-09
    • 1970-01-01
    • 2021-02-26
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多