Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move auto installation code into the store constructor #914

Merged
merged 1 commit into from
Sep 1, 2017

Conversation

ktsn
Copy link
Member

@ktsn ktsn commented Aug 18, 2017

While auto-calling Vue.use(Vuex) is convenient for users who use Vue/Vuex via <script>, there are a case that users want to avoid the behavior when window.Vue is exists (See #731).

Currently, Vuex is immediately installed into window.Vue when Vuex is imported. So there are no way to install Vuex into imported Vue.

To solve this, I'd move it in Vuex.Store's constructor. Then the auto-installation is delayed until users create a store and the users can install Vuex into their own Vue constructor before doing it.

I'm not sure how to write test code for this but it looks like working well.

fix #731

@yyx990803
Copy link
Member

Since the constructor can be called multiple times, we need to make sure install is called only once.

@ktsn
Copy link
Member Author

ktsn commented Aug 18, 2017

I thought existence check of inner Vue is enough because when install is called, the inner Vue will be assigned. Then install will be skipped on the next time.
Am I missing any other cases?

@yyx990803
Copy link
Member

yyx990803 commented Aug 18, 2017

Ah right, nevermind! LGTM

@ianrussel
Copy link

ianrussel commented Aug 19, 2017

I have same problem
[vuex] already installed. Vue.use(Vuex) should be called only once.

When using window.Vue

I fixed it by
bootstrap.js

 window.Vue = require('vue');
  window.Vuex = require('vuex');
  Vue.use(Vuex); 

main.js

      import store from './store/store.js';
      import Papp from './Papp';

     import VueRouter from  'vue-router';

     const router = new VueRouter({
         //mode: 'history',
     routes: [
      {
         path: '/vue/dashboard',
         component: dashboard
      },
     {
         path: '/vue/dashboard/love',
         component: love,
     }
     ]
    })

     const vm = new Vue({
        el:'#vue',
        router,
       store,
        render: h =>h(Papp),
    });

store.js

         export default new Vuex.Store({
             state: {
                   todos: [],
                   newTodo: '',
             },
           mutations: {
              GET_TODO(state, todo) {
                   state.newTodo = todo
            },
         },
         actions: {
             getTodo({commit}, todo){
                     commit('GET_TODO', todo)
             },
            addTodo({commit}){
               commit('ADD_TODO')
           },
         .................
          },
         getters: {
                  newTodo: state => state.newTodo,
                 todos: state => state.todos.filter((todo) => {return   !todo.completed}),
                 completedTodos: state => state.todos.filter((todo) => {return todo.completed})
                  }
       })

@yyx990803 yyx990803 merged commit 852ac43 into vuejs:dev Sep 1, 2017
@ktsn ktsn deleted the fix-global-install-issue branch September 1, 2017 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[vuex] already installed. Vue.use(Vuex) should be called only once.
3 participants