[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?
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 register
and dispatch
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
vuex
in addition to providing global variables, it can also:
store.dispatch ('actionName')
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')
vuex
, the answer is obvious! Previous: PHP was separated anterior and posterior. How to do login verification code
Next: How to deal with some values when dealing with business logic?
when namespaced is not used, actions is registered globally by default, but after use, actions will become under the module, but I don t want to do this. I also want it to be global. My namespace:true is just for the convenience that I introduce state, ...
there are more than 10 methods for methods in one of my .vue files, and this page looks messy. is it because my componentization is not thorough enough; is there any way to make the file more concise ...
I want a variable in the drop-down box v-model data, and the default value of this variable is the constant defined in state, but what if it doesn t change with the value in state? reference: https: codeshelper.com q 10. The value of computed canno...
as shown in the figure, I get the data in vuex in created. I can get it as soon as I enter the page, but I can t get the data after the page is refreshed ...
problem description I currently have a parent component, which has more than 20 child components A1, a2, and so on, and I want them to update their balances. What s the best way to deal with it. the environmental background of the problems and what...
state.js state:{ data:{} } mutation.js type.data (state, arg) { return state.data = arg } action.js commit(types.data, {a: 111, b: 222}) getters.js export const data = (state) => { return state.data } is called in the methods method o...
the code is as follows: import headerVue from ". components header.vue"; import bodyVue from ". components body.vue"; import footerVue from ". components footer.vue"; import connector from ". components connector.js&q...
model: there are two ways: 1. this.getData().then(data => { data; }) Mode 1 is to save a state in vuex , and in many cases, I first call updateData to make sure that data in state is up-to-date. method 2 does not need state , but ...