反应值未定义

时间:2018-05-01 08:58:22

标签: vue.js vuejs2

我不知道为什么我无法在方法中获取反应值。

...
<div class="card">
  <div class="card-contents">
    <datafieldcheckbox class="filterComponents" :filtervalue="filterAll" @call-method="callfilteredproducts"></datafieldcheckbox>
  </div>
</div>
....
    new Vue({
            el: "#app",
            data() {
                return {
                    filterAll: this.filtered(),
                    dataCategory : ["data"]
                }
            },
.....
    methods: {
                filtered() {
                    console.log("this.data", this.dataCategory)  // Got undefined insted of getting value.
                }
...

1 个答案:

答案 0 :(得分:1)

调用filtered方法时,data尚未完全设置。 dataCategory不可用是有道理的。相反,请在created挂钩中调用它,数据已经可用。

export default {
  data() {
    return {
      filterAll: null,
      dataCategory: ["data"]
    };
  },
  methods: {
    filtered() {
      console.log("this.data", this.dataCategory); // Got undefined insted of getting value.
    }
  },
  created() {
    this.filterAll = this.filtered();
  }
};

(P.S。不确定你想要实现的目标。但这似乎不对。)