VueJS-Vue未定义

jed*_*edi 2 vue.js vue-resource vuex

我一直挑战自己编写一个应用程序,该应用程序可从API获取数据并在各种组件中显示数据。我对VueJS很陌生。我使用VueResource来访问API,使用VueX来进行状态管理。我已经设置了商店,添加了动作,变异和created获取方法等,并且在组件中添加生命周期方法后,我立即收到错误消息:

ReferenceError: Vue is not defined
    at Store.eval (eval at <anonymous> (build.js:1017), <anonymous>:11:3)
    at Array.wrappedActionHandler (eval at <anonymous> (build.js:1338), <anonymous>:711:23)
    at Store.dispatch (eval at <anonymous> (build.js:1338), <anonymous>:433:15)
    ...
Run Code Online (Sandbox Code Playgroud)

我的代码如下所示:

main.js

import Vue from 'vue'
import App from './App.vue'
import VueResource from 'vue-resource'
import store from './store/store'

Vue.use(VueResource);

new Vue({
  el: '#app',
  store,
  render: h => h(App)
})
Run Code Online (Sandbox Code Playgroud)

store.js

import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import mutations from './mutations'
import getters from './getters'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    products: []
  },
  getters,
  actions,
  mutations
})

export default store
Run Code Online (Sandbox Code Playgroud)

应用程序

<template>
   ...
</template>

<script>
    import { FETCH_MEALS } from './store/types';

    export default {
      //load meals on page load
      created() {
        this.$store.dispatch(FETCH_MEALS)
      },
      computed: {
        meals() {
          return this.$store.getters.meals
        },
        salads() {
          return this.$store.getters.salads
        },
        lunches() {
          return this.$store.getters.lunches
        },
        starters() {
          return this.$store.getters.starters
        }
      }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

而且我被卡住了,我不知道我在做什么错。你有什么想法?

更新

我使用由vue-cli生成的典型样板,并使用Webpack构建main.js。

actions.js

import { API_ROOT } from '../config'
import * as types from './types';

export default {
  [types.FETCH_MEALS]: ({commit}) => {
    Vue.http.get(API_ROOT + '/meals.json')
    .then(response => response.data)
    .then(meals => {
      commit(types.SET_MEALS, meals)
    })
  }
};
Run Code Online (Sandbox Code Playgroud)

突变.js

import * as types from './types';

export default {
    [types.MUTATE_UPDATE_VALUE]: (state, payload) => {
        state.value = payload;
    }
};
Run Code Online (Sandbox Code Playgroud)

getters.js

import * as types from './types';

export default {
    [types.VALUE]: state => {
        return state.value;
    }
};
Run Code Online (Sandbox Code Playgroud)

Bob*_*ger 6

我的猜测是,你使用Vue(像Vue.set())内没有上市 actions.jsmutations.js或者getters.js,却忘了补充:

import Vue from 'vue'
Run Code Online (Sandbox Code Playgroud)

在该文件的开头。