Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vuex 子组建获取store中state为undefined,为啥呢?

vuex 子组建获取store中state为undefined,main.js中import store 后在footer.vue用this.$store.state.count获取值为undefined,但是如果把store里定义的store直接粘贴到main.js里面,在footer.vue里就可以获取到值,为什么为什么为什么为什么???????

main.js

import Vue from 'vue';
import App from './App.vue';
import VueRouter from "vue-router";
import VueResource from 'vue-resource';
import Vuex from 'vuex';
import store from './vuex/store';

//开启debug模式
Vue.config.debug = true;

Vue.use(VueRouter);
Vue.use(VueResource);
Vue.use(Vuex);

import Home from './component/home.vue';
import Login from './component/login.vue';
import First from './component/first.vue';

// 创建一个路由器实例
// 并且配置路由规则
const router = new VueRouter({
        mode: 'history',
        base: __dirname,
        routes: [{
            ....//?路由配置
        }]
    })
    
new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App)
})

store.js

import Vue from 'Vue';
import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  getter:{
      
  }
})
module.exports = store

footer.vue

<template>
    <div id="footer">{{count}}</div>
</template>

<script type="text/javascript">
    import Vue from 'vue'
    import {
        mapGetters
    } from 'vuex'
    export default {
        data() {
                return {
                }
            },
            computed: {
                count() {
                    return this.$store.state.count;
                }

            }
    }
</script>```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

store.js里的vue import 错误,应该是import Vue from vue,大小写问题


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...