代码之家  ›  专栏  ›  技术社区  ›  netik

VueJS计算数据和范围

  •  0
  • netik  · 技术社区  · 8 年前

    我正在尝试创建一个数组 elections 选举 我的数据存储。

    export default {
        computed: {
            data() {
               var elections = [];
               this.$dataStore.state.elections.forEach((el) => {
                   console.log(this);
                   var election = { id:el.id, icon: 'assignment', iconClass: 'blue white--text', title: el.title, subtitle: 'Jan 20, 2018' };
                   this.elections.push(election);
               }); 
    
               return {
                  elections: this.elections
               }
           }
        }
    }
    

    1 回复  |  直到 8 年前
        1
  •  2
  •   thanksd thibaut noah    8 年前

    引用 this.elections data 方法返回将不起作用,因为尚未设置Vue实例的数据属性。

    因为你想做什么。只需参考 elections forEach .

    还有,你有你的 数据 中的方法 computed 对象,但它应该在它之外。

    push 而不是 put 添加到阵列时。

    export default {
      data() {
        var elections = [];
        this.$dataStore.state.elections.forEach((el) => {
          var election = { id:el.id, icon: 'assignment', iconClass: 'blue white--text', title: el.title, subtitle: 'Jan 20, 2018' };
          elections.push(election);
        }); 
    
        return {
          elections: elections
        }
      }
    }