This is an intro-talk how to handle state in Vue.js applications.
It shows briefly the different alternatives Single Component, Parent-Child, Event Bus, Simple Global State and finally Vuex
It is accompanied by a live demo and a code repository.
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6
msg | January 2019 | Handling State in Vue.js Applications | Alexander Schwartz 4 export default { data () { return { person: { firstname: "Theo", lastname: "Tester" } } }, /* ... */ } Everything in the View-Model is State
msg | January 2019 | Handling State in Vue.js Applications | Alexander Schwartz 5 We need state • to display information • to validate and act on data • to share with other component export default { data () { return { person: { firstname: "Theo", lastname: "Tester" } } }, /* ... */ }
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6
| January 2019 | Handling State in Vue.js Applications | Alexander Schwartz 10 Common: • all components read from the same model (passed on via props) • displayed values change in all components (based on Vue.js’ reactivity) Alternatives for Model Ownership: • Read Only: Child Components only read data, but never modify • Global Model: All components allowed to write (bad practice) • Events-Model fine grained: Childs fires events on single key strokes on each field • Events-Model coarse grained: Children fires events after validation of multiple fields Parent Child 1 Child 2
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6
| January 2019 | Handling State in Vue.js Applications | Alexander Schwartz 13 • State is within the components • Components send events to the Event Bus Challenges: • Order of component creation might be undefined • Not using Vue.js’ reactivity model Component 2 Component 1 Event Bus
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6
2019 | Handling State in Vue.js Applications | Alexander Schwartz What is State 1 Minimal: Single Component 2 Hierarchy: Parent-Child 3 Horizontal: Event Bus 4 Inversion of State: Simple Global State 5 Inversion of State: Vuex 6