2017-09-26 70 views
0

我有希望將數據提交到vuex突變的VUE方法,由於某種原因,我不斷收到遺漏的類型錯誤:這$ store.commit不是功能

的錯誤,當我點擊列表項,調用函數觸發。


sample.vue

<li class="tabs-title" v-for="item in filteredItems" v-on:click="upComponents" :key="item.initials" > 

export default { 
    data() { 
    return { 
     search: null, 
    }; 
    }, 
    computed: { 
    filteredItems() { 
     const coins = this.$store.state.coin.coin; 
     if (!this.search) return coins; 

     const searchValue = this.search.toLowerCase(); 
     const filter = coin => coin.initials.toLowerCase().includes(searchValue) || 
      coin.name.toLowerCase().includes(searchValue); 

     return coins.filter(filter); 
    }, 
    }, 

    methods: { 
    upComponents(item) { 
     this.$store.commit('updatedComp', item); 
    }, 
    }, 

    mounted() { 
    this.tabs = new Foundation.Tabs($('#exchange-tabs'), { 
     matchHeight: false, 
    }); 
    }, 
    destroyed() { 
    this.tabs.destroy(); 
    }, 
}; 

這是store.js文件,其中我宣佈突變。

import Vue from 'vue'; 
import Vuex from 'vuex'; 
import coin from '../data/system.json'; 

Vue.use(Vuex); 

export default { 
    state: { 
    coin, 
    selectedCoin: 'jgjhg', 
    }, 
    mutations: { 
    updatedComp(state, newID) { 
    state.selectedCoin.push(newID); 
    }, 
    }, 
    getters: { 
    coin: state => state.coin, 
    }, 
}; 

main.js,這就是我宣佈Vue的應用

import jQuery from 'jquery'; 
import Vue from 'vue'; 
import App from './App'; 
import router from './router'; 
import store from './store/store'; 


window.jQuery = jQuery; 
window.$ = jQuery; 

require('motion-ui'); 
require('what-input'); 
require('foundation-sites'); 


new Vue({ 
    el: '#app', 
    store, 
    router, 
    template: '<App/>', 
    components: { App }, 
}); 

這是網頁我的工作,我在那裏加載的所有組件:

<template> 
    <div class="grid-container"> 
    <div class="grid-x"> 
     <div > 
     <headline-exchange></headline-exchange> 
     ... 
     </div> 
    </div> 
    </div> 
</template> 



<script> 
import Headline from './molecules/Headline'; 

export default { 
    components: { 
    'headline-exchange': Headline, 
    }, 
}; 

</script> 
+0

您使用的模塊系統?如果是這樣,你是否添加了'Vuex'作爲插件,即'Vue.use(Vuex)'?請參閱store.js中的https://vuex.vuejs.org/en/installation.html – Phil

+0

,是的。 – lopezi

+0

你把'store'注入了'Vue'實例,例如'new Vue({...,store,...})'嗎?見https://vuex.vuejs.org/en/state.html#getting-vuex-state-into-vue-components – Phil

回答

1

你是不是創建一個Vuex店。您擁有的只是一個定義商店屬性的對象。

更改store.js

export default new Vuex.Store({ 
    state: { ... }, 
    mutations: { ... }, 
    // etc 
}) 

https://vuex.vuejs.org/en/getting-started.html#the-simplest-store

+0

是正式解決了,我問即使艱難,現在我得到了另一個類似的:)遺漏的類型錯誤的問題:state.selectedCoin.push不是一個函數 – lopezi

+0

@lopezi這是一個簡單的一,'selectedCoin'是一個字符串,而不是一個數組 – Phil

相關問題