Vue状态变量未更新

时间:2019-06-19 10:34:50

标签: vue.js vuejs2

我有这段代码,但是当调用selectCategory方法时,在vue开发人员控制台中看不到product.categories正在更新。

  <li class="border hover:bg-blue-100" v-bind:class=""
                    v-on:click="selectCategory($event, category.id)"
                    >
                    {{category.name}}
  </li>

 export default {
  data () {
    return {
      product: {
        categories: []
      },
    }
  },
  methods: {
    selectCategory(event, id){
      this.product.categories.push(id);
    },
}

1 个答案:

答案 0 :(得分:0)

     This might help you as referance.  

        <template>
         <div>
           <li v-for="category in Available.categories" 
               class="border hover:bg-blue-100" 
               v-bind:class=""                          
               v-on:click="selectCategory($event, category.id)"
           >
              {{category.name}}
          </li>
          </div>
       </template>

       <script>

         export default{
          data () {
            return {
              Available: {
                categories: [{
                  "name":"Fruites",
                  "id":1,
                },
                {
                  "name":"Veges.",
                  "id":2,
                }]
              },
               product: {
                categories: []
              },

            }
          },
          methods: {
            selectCategory(event, id){
              this.product.categories.push(id);
              console.log(this.product.categories)
            },
          }
        }
        </script>

https://codesandbox.io/s/vue-template-v2zss?fontsize=14