鍍金池/ 問答/HTML/ vue-cli項目中的一些問題請教!

vue-cli項目中的一些問題請教!

<template lang="html">

  <div class="cartcontrol">
    <transition name="fadeRotate">
      <div class="cart-decrease" v-show="food.count>0" @click.stop.prevent="decreaseCart()">
          <span class="icon-remove_circle_outline inner"></span>
      </div>
    </transition>
    <div class="cart-count" v-show="food.count>0">
      {{food.count}}
    </div>
    <div class="cart-add" @click.stop.prevent="addCart($event)">
      <i class="icon-add_circle"></i>
    </div>
  </div>

</template>

<script>
import Vue from 'vue'

export default {
  props: {
    food: Object
  },
  methods: {
    addCart(event) {
      console.log(event.target);
       if (!event._constructed) {
         return
       }
      if (!this.food.count) {
        Vue.set(this.food, 'count', 0)
      }
      this.food.count++;
      this.$root.eventHub.$emit('cart.add', event.target)
    },
    decreaseCart() {
      // if (!event._constructed || !this.food.count) {
      //   return
      // }
      this.food.count--;
    }
  }
}

</script>

想了解一下下面這幾句是什么意思 什么情況下使用 功能是干嘛的
Vue.set(this.food, 'count', 0)
this.$root.eventHub.$emit('cart.add', event.target)
if (!event._constructed) {

     return
   }
回答
編輯回答
愚念

1: set 設(shè)置this.food下面的count 為0
clipboard.png
2: this.$root 指向當(dāng)前組件的根vue實例 ,如果沒有就是自身 $emit 監(jiān)聽當(dāng)前實例上事件事件,可以由vm.$emit觸發(fā)?;卣{(diào)函數(shù)會接收所有傳入事件觸發(fā)函數(shù)的額外參數(shù)。

3: event 是行參 是指的是$event 當(dāng)前元素的event對象
https://cn.vuejs.org 官網(wǎng)都可以看到解釋

2018年3月11日 21:44