下面的簡單代碼給了我很多頭痛,因爲some-component和root實例都向控制檯記錄錯誤,而不是從vuex對象進行綁定。我可能會在這裏錯過什麼?Vuex不能在子組件中工作
var state = {
\t counter: 0
};
var mutations = {};
var store = new Vuex.Store({state, mutations});
var someComponent = Vue.extend({
template: '<div>{{counter}}</div>',
//data: function(){return {counter: 0}},
vuex: {
getters: {
counter: getCounter
}
}
});
new Vue({
el: '#app',
components: {
\t 'some-component': someComponent
},
store: store,
vuex: {
\t getters: {
\t \t counter: getCounter
\t }
}
});
function getCounter(state) {
return state.counter;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>vue-bug</title>
</head>
<body>
<div id="app">
\t <span>{{counter}}</span>
\t <some-component></some-component>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.0-rc.1/vue.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vuex/2.0.0-rc.1/vuex.js"></script>
</body>
</html>
在我的代碼,我打電話Vue.use(Vuex),但在這種撥弄我沒有(它說,Vuex已註冊)。此外,請注意,如果您取消註釋該行的數據,該組件呈現正確。
任何幫助,非常感謝。
如果您使用'Vue 2.0',您應該使用'Vuex 2.0'。我不能100%確定Vue.js 2.0與您使用的Vuex版本兼容。你能升級到Vuex 2.0嗎? 你可以在你的組件中添加'created'鉤子並執行'console.log(this。$ store)'來查看你的商店是否被注入到你的組件中? 提供jsFiddle,它更容易調試,然後查看上面的代碼。 –
@PrimozRome我大量編輯了我的原始問題,以便重現和更新vuex版本。此外,'console.log(this。$ store)'告訴我商店正在被注入。謝謝! – Ozymas