[vue] Why use vuex instead of window.obj to maintain global instance properties and methods?

[vue] Why use vuex instead of window.obj to maintain global instance properties and methods?

Jul.20,2021

then why don't you use native js to develop vue ?
Let's take window.obj as an example of using development

window.obj = {
    count: 1,
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
}

if you want to modify the status

//a
window.obj.count+=1;
//b
window.obj.count+=5;
//c
window.obj.count=10;
The above method certainly won't work. In a complex program, you have to encapsulate it into a method to modify
, and then you have register and dispatch
for devtools to facilitate tracking and debugging vuex make your own encapsulation Mutation commit so that the status must be submitted by Synchronize in js , so there must be async in Action < code.

like the one above, you will find that you encapsulate vuex or similar state management

while doing so.

vuex in addition to providing global variables, it can also:

  • abstract business logic and reduce repetitive code. For example, for data acquired asynchronously, you don't need to write code for the request interface everywhere, just store.dispatch ('actionName')
  • uniform data source; a data may come from: 1. Server 2. web socket push 3. Operator operations performed on data after responding to user actions; instead of using vuex , you have to write code to get / modify data in one of these three places, while using vuex requires only one sentence store.dispatch ('actionName')

whether to use vuex , the answer is obvious!


  1. relative security
  2. Modular Management
  3. responsive
Menu