2017-08-30 61 views
0

我試圖測試我的$on函數是否正常工作,但在事件$ emit發出時,Vue $未運行我的函數。我可以清楚地看到Vue控制檯正在接收事件發送,但$on中的預定義回調函數未被調用。即使我能看到在Vue控制檯觸發的事件

下面是代碼:

<template lang="html"> 
    <div class="test"> 
    <Filters></Filters> 
    <div> 
    <ul class="o-list c-stores"> 
     <Result v-bind:total="filteredRestuarants.length" v-bind:open="isOpen" v-on:toggle="toggleRestaurantList"></Result> 
     <li v-for="(restaurant, index) in filteredRestuarants" class="c-stores__location" :class="{'first': isFirst(index), 'last': isLast(index, restaurants)}"> 
     <Location :index="index" :store="restaurant" :link="() => setCurrentRestaurant(restaurant)"></Location> 
     </li> 
    </ul> 
    </div> 
    </div> 
</template> 

<script> 
import eventHub from './../../event-hubs/storefinder' 
import Location from './Location' 
import Filters from './Filters' 
import Result from './Result' 

export default { 
    props: ["restaurants", "isOpen", "currentSearch"], 
    data() { 
    return { 
     attributes : [], 
    } 
    }, 
    head: { 
    title: function() { 
     return { 
     inner: this.$t('storefinder.overview') 
     } 
    }, 
    meta: function functionName() { 
     return [{ 
      name: 'og:title', 
      content: this.$t('storefinder.overview') + ' - ' + this.$t('storefinder.name'), 
      id: "og-title" 
     }, 
     { 
      name: 'description', 
      content: this.$t('storefinder.description'), 
      id: "meta-description" 
     }, 
     { 
      name: 'og:description', 
      content: this.$t('storefinder.description'), 
      id: "og-description" 
     }, 
     ] 
    } 
    }, 
    components: { 
    Location, 
    Filters, 
    Result 
    }, 
    computed: { 
    filteredRestuarants(rest) { 
     let restaur = rest || this.restaurants; 
     return this.restaurants; 
    } 
    }, 
    methods: { 
    startEvents(){ 
     eventHub.$on('addFilterTheRestaurants', (attribute) => {console.log("test")}); 
     eventHub.$on('removeFilterTheRestaurants', (attribute) => {console.log("test")}); 
    }, 
    toggleRestaurantList() { 
     eventHub.$emit('showRestaurantList'); 
    }, 
    setCurrentRestaurant(restaurant) { 
     this.trackRestaurantSelect(restaurant.publicNameSlug); 
     this.$router.push({ 
     name: "store", 
     params: { 
      restaurant: restaurant.publicNameSlug 
     } 
     }); 
    }, 
    trackRestaurantSelect(restaurantName) { 
     dataLayer.push({ 
     'event': 'GAEvent', 
     'eventCategory': 'restaurants', 
     'eventAction': 'clickResult', 
     'eventLabel': restaurantName, 
     'eventValue': undefined, 
     'searchTerm': this.currentSearch && this.currentSearch.toLowerCase(), 
     'amountSearchResults': 1 
     }); 
    }, 
    created() { 
     this.startEvents() 
     // eventHub.$on('addFilterTheRestaurants', (attribute) => this.filteredRestuarants.value = this.restaurants.forEach(rest => {console.log(rest)})); 
     // eventHub.$on('addFilterTheRestaurants', (attribute) => {console.log("test")}); 
     // eventHub.$on('removeFilterTheRestaurants', (attribute) => {console.log("test")}); 
    }, 
    beforeDestroy() { 
     bus.$off('addFilterTheRestaurants') 
     bus.$off('removeFilterTheRestaurants') 
    }, 
    isLast: function (idx, list) { 
     return idx === list.length - 1; 
    }, 
    isFirst: function (idx) { 
     return idx === 0; 
    }, 
    } 
} 
</script> 

ADN其正在這裏emited:

<template lang="html"> 


<div class="c-filters"> 
    <div class="c-filters-list"> 
    // Here I call the function to $emit my event 
     <div class="c-filters__item" v-for="item in filters" @click="(e) => {toggleClass(e); addFilter(item)}"> 
     {{$t(`storefinder.store.attributes.${item}`)}} 
     </div> 
    </div> 
    </div> 
</template> 

<script> 
import {getKeys} from './../../i18n/' 
import eventHub from './../../event-hubs/storefinder' 
import notificationHub from './../../event-hubs/notification' 
import GLSApi from './../../api/gls' 

export default { 
    data() { 
    return { 
     attributes: null, 
     filters : [ 
     "WIFI", 
     "TABLESERVICE", 
     "MCCAFE", 
     "INDOORDINING", 
     "DRIVETHRU", 
     "BREAKFAST", 
     "MALEFEMALEHANDICAPACCESSIBLE", 
     "BABYCHANGING", 
     "BIRTHDAYPARTIES", 
     "SELFORDERKIOSK", 
     "OUTDOORPLAYGROUND", 
     "INDOORPLAYGROUND", 
     "JUNIORCLUB" 
     ] 
    } 
    }, 
    computed: { 
    getAttributes() { 
     let arr = this.attributes.map(elem => elem.storeAttributes.attribute) 
     return arr.map((el,index, array) => array[index].map(obj => obj.type)) 
    } 
    }, 
    methods: { 
    toggleClass(e){ 
     e.target.classList.contains("add-filter") ? e.target.classList.remove("add-filter") : e.target.classList.add("add-filter") ; 
    }, 
// here it the $emit happening 
    addFilter (item) { 
     eventHub.$emit('addFilterTheRestaurants', item); 
    }, 
    deleteFilter (item) { 
     eventHub.$emit('removeFilterTheRestaurants', item); 
    } 
    }, 
    beforeCreate() { 
    eventHub.$on('attributesLoaded', (params) => this.attributes = params); 
    } 
} 
</script> 
+0

您在'startEvents'計算屬性中註冊處理程序,但是您是否在任何地方使用該屬性? – thanksd

+0

那麼''startEvents'從'calculate'屬性中被自動調用,不是嗎?爲什麼我會看到這個事件,當我$發出它onClick時,你可以在第二個文件 – Jousi

回答

1

您在計算性能進行了註冊你addFilterTheRestaurants事件。但是,除非計算屬性被引用,否則不會調用計算屬性的函數。由於你永遠不會引用this.startEvents,函數永遠不會執行。

您可以整天發出addFilterTheRestaurants事件,但沒有事情發生,因爲您尚未註冊該事件。

只需在created鉤子中註冊$on處理程序。或者使用startEvents而不是計算屬性,並在created鉤子中調用它。但是,請確保您指定了created生命週期掛鉤,而不是名爲created的方法。

+0

中看到我把它放在'created()'方法之前,它也沒有工作,這就是爲什麼我嘗試了它'計算'。我現在已經將它轉換爲'methods',並從'created()'調用它,但它仍然不是控制檯記錄任何東西:( – Jousi

+0

而且您沒有在控制檯中發現任何錯誤? – thanksd

+0

控制檯和Vue事件中沒有任何內容瀏覽器觀察者正在看到事件被調用 – Jousi

相關問題