vueschool.io
Rolf Haug
Teacher @ Vue SchoolΒ
Full Stack developer
Consultant
Foodie πΆ
Oslo, Norway
Alex
Kyriakidis
Teacher @ Vue School
Author of The Majesty of Vue.jsΒ 1 & 2
Vue.js Contributor
Consultant @ BuildingLink
Amsterdam
280 lessons β’Β 59.000+ usersΒ
Who's using Vuex?
Workshop Structure
Buddy System
β π π¨βπ«π©βπ«
What is Vuex?
State Management Pattern
& Library for Vue.js applications
Centralized Store
Enforces rules that results in predictable behavior
What does Vuex solve?
π§ββοΈ
Vuex was developed to fight zombies!
Vuex is based on Flux
Pattern made by Facebook engineers
The Notorious Zombie Bug
Back-and-forth betweeen being resolved and an issue
Facebook wanted a predictable app
The underlying issue
The Data Flow
When the app grows
Quite messy!
The Data Flow
Consequentially invoked many updates
Asynchronous updates
returned wrong or corrupt data.
π§ββοΈ
Causing the Zombie Bug
Flux Pattern
Ensures a one way data flow
Action
Dispatcher
Store
View
Shopping Cart
Example
Action
Dispatcher
Store
View
User clicks add to cart in view
Action organize data
Dispatches to the Store
View reads from the Store
Flux Pattern
Ensures a one way data flow
Results in predictable behavior
Clear text? or Greek?
Action
Dispatcher
Store
View
Don't worry!
π€
Data sharing
The Flux pattern makes it easy to share data between components
Communicating with props and events is perfectly fine
Some apps benefit from having a centralized store
π
π
Centralized Store
Works like an event bus/hub in many ways
We do not have to "guide" events through components anymore
Flux
Just a pattern
Not a framework
Popular Frameworks implements the Flux Pattern
React β Redux
Vue β Vuex
What is State?
State is your application data
Imagine a components data globally available
The Store
The store is the global component that holds our data - the application state
export default {
data() {
return {
products: [...]
}
}
}
import Vuex from 'vuex'
export default new Vuex.Store({
state: {
products: [...]
}
})
Component vs Vuex Store
The store is the "component"
The state is the "data"
What should we store?
Vuex solves certain problems, but you should not store everything
Do you need the data to be globally available?
Yes β Vuex Store
No β Components data
- Authenticated User
- Blog posts
- Products
- toggle state
- Form data
- Loading state
Global vs local data
Makes sense?
npm install vuex --save
You can install Vuex with the node package manager
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
Import and use in our app
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export new Vuex.Store({
state: {
// Top 3 movies of all time according to IMDB
movies: [
{id: 1, name: 'The Shawshank Redemption'},
{id: 2, name: 'The Dark Knight'},
{id: 3, name: 'The Godfather'}
]
}
})
Create the Store
The Store can be stored anywhere
~/store.js
import store from '@/store'
~/store/index.js
// main.js
import Vue from 'vue'
import store from '@/store'
import router from '@/router'
import App from '@/App'
new Vue({
store,
router,
el: '#app',
components: {
App
},
template: '<App/>'
})
Inject the Store
// MoviesList.vue
export default {
computed: {
movies () {
return this.$store.state.movies
}
}
}
Access the store under this.$store
Wrap the state in computed properties
-
State is reactive
-
Computed properties reevaluate on state change
-
Changes will be reflected in all components
Summary
Vuex makes it easy to share data between multiple components
The store is the single source of truth
Questions?
π©βπ»π¨π½βπ» Up and Running
- download https://bit.ly/2UT5zSh
- install dependencies (yarn or npm install)
π©βπ»π¨π½βπ» Up and Running
π©βπ»π¨π½βπ» Assignment (20 mins)
-
install Vuex and create store
Β -
copy the products array from api/shop.js to vuex state
Β -
make app aware of the Vuex store (pass store option to main.js)
Β -
use state in the Home.vue to display all products
Β - use state in Product.vue to display the product with the specified id
1. Vuex Intro
By Rolf Haug
1. Vuex Intro
- 335