简体   繁体   English

Vue 2 组件道具得到错误的值

[英]Vue 2 component prop getting wrong value

I am trying to build a menu between categories.我正在尝试在类别之间建立一个菜单。 If a category has a sub-category it returns a value that says has_subCategory as boolean 0/1.如果一个类别有一个子类别,它会返回一个值,将 has_subCategory 表示为布尔值 0/1。

<template>
    <select><slot></slot></select>
</template>

<script>

    export default {

      props: ['value',
              'hasSubCat'],
      watch: {
        value: function(value, hasSubCat) {
          this.relaod(value);
          this.fetchSubCategories(value, hasSubCat);
        }
      },
      methods: {
        relaod: function(value) {

          var select = $(this.$el);

          select.val(value || this.value);
          select.material_select('destroy');
          select.material_select();

        },
        fetchSubCategories: function(value, hasSubCat) {
          var mdl = this;
          var catID = value || this.value;
          var has_subCat = hasSubCat || this.hasSubCat;

          console.log("has_subCat:" + has_subCat);

          mdl.$emit("reset-subcats");

          if (catID) {
            if (has_subCat == 0) {
              if ($('.subdropdown').is(":visible") == true) {
                $('.subdropdown').fadeOut();
              }
            } else {
              axios.get(URL.API + '/subcategories/' + catID)
                .then(function(response) {
                  response = response.data.subcatData;
                  response.unshift({
                    subcat_id: '0',
                    subcategory_name: 'All Subcategories'
                  });
                  mdl.$emit("update-subcats", response);

                  $('.subdropdown').fadeIn();
                })
                .catch(function(error) {
                  if (error.response.data) {

                    swal({
                      title: "Something went wrong",
                      text: "Please try again",
                      type: "error",
                      html: false
                    });

                  }
                });
            }
          } else {
            if ($('.subdropdown').is(":visible") == true) {
              $('.subdropdown').fadeOut();
            }
          }
        }
      },
      mounted: function() {

        var vm = this;
        var select = $(this.$el);

        select
          .val(this.value)
          .on('change', function() {
            vm.$emit('input', this.value);
          });

        select.material_select();
      },
      updated: function() {

        this.relaod();
      },
      destroyed: function() {

        $(this.$el).material_select('destroy');
      }
    }
</script>


<material-selectcat v-model="catId" name="category" @reset-subcats="resetSubCats" @update-subcats="updateSubCats" id="selcat">
                    <option v-for="cat in cats" :value="cat.cat_id" :hasSubCat="cat.has_subCat" v-text="cat.category_name"></option>
                  </material-selectcat>

The data looks like this:数据如下所示:

cat_id:"0"
category_name:"All Subcategories"
has_subCat:0

What I dont understand is that console.log("has_subCat:" + hasSubCat);我不明白的是console.log("has_subCat:" + hasSubCat); prints out different values each time I change the select.每次更改选择时打印出不同的值。 It should only display 0 or 1它应该只显示01

Watcher in vue.js is supposed to be used in order to watch one value, but you can fulfill your requirement with help of computed . vue.js 中的 Watcher 应该用于观察一个值,但您可以computed的帮助下满足您的要求。

export default {
  props: ['value',
          'hasSubCat'],
  watch: {
    /* without this, watcher won't be evaluated */
    watcher: function() {}
  },
  computed: {
    watcher: function() {
      this.reload(this.value);
      this.fetchSubCategories(this.value, this.hasSubCat);
    }
  },
  ...
}

I also made a simplified working fiddle , you can have a look.我还做了一个简化的工作小提琴,你可以看看。

You are assuming that the second parameter of your watch function is hasSubCat which is not the case.您假设 watch 函数的第二个参数是hasSubCat ,但事实并非如此。 While the first parameter of the value watch function represents the new value of the property, the second parameter is actually the old value of the watched property. value watch 函数的第一个参数表示属性的新值,而第二个参数实际上是被监视属性的旧值。 Try this out to understand more.试试这个以了解更多。

watch: {
   value: function(value, oldValue) {
     console.log('new value:', value)
     console.log('old value:', oldValue)
   }
}

So to watch both of value and hasSubCat , you can do something like this:因此,要同时查看valuehasSubCat ,您可以执行以下操作:

watch: {
  value: function(newValue) {
     this.reload(newValue)
     this.fetchSubCategories(newValue, this.hasSubCat)
  },
  hasSubCat: function(newHasSubCat) {
     this.reload(this.value)
     this.fetchSubCategories(this.value, newHasSubCat)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM