Vuex存储中的vue.js变量在组件中不可访问

时间:2017-09-20 04:20:03

标签: javascript vue.js vuex

我在Vuex BASE_URL

中定义了store/store.js
export const store = new Vuex.Store({
    state: {    
        BASE_URL: 'http://127.0.0.1:8090',
        isAuthenticated: false
    }, 
    ...

并将商店导入main.js

import Vue from 'vue'
import App from './App.vue'
import VueResource from 'vue-resource'
import VueRouter from 'vue-router'
import Routes from './routes'
import {store} from './store/store'


Vue.use(VueResource)

Vue.use(VueRouter);

const router = new VueRouter({
    routes: Routes,
    mode: 'history'
});


new Vue({
  store: store,
  el: '#app',
    router: router,

  render: h => h(App)
});

但是,当我尝试在组件中导入BASE_URL时:

    computed: {
        BASE_URL () {
        return this.$store.state.BASE_URL;  
      }

  },
  methods: {

   login: function () {
            axios.post( BASE_URL + "/api/login", {
            username: this.username,
            password: this.password,
          }).then(function(data){
            console.log(data);
          });

      },

我收到此错误:

Uncaught ReferenceError: BASE_URL is not defined
    at VueComponent.login (webLogin.vue?e2e5:175)
    at Proxy.boundFn (...

这里有什么问题?如何解决?

1 个答案:

答案 0 :(得分:0)

方法Stream中的代码中有拼写错误:

login