A Vue.js app with Vuex: a centralized state manager for Vue
This repository is an example of web application developed using Vue.js that integrates Vuex: a state management pattern library for Vue.js applications.
If you are new to Vue's world or if you want to understand Vuex's concepts, this repository is for you. 😃
You must have Npm or Yarn installed on your machine.
This project was generated with the Vue CLI. If you want to develop you personal project with Vue, install the Vue CLI
Clone the repository using Git:
git clone https://github.com/dj0nny/vuejs-state-management-for-beginners.gitOr download the repository here
Run
npm run install
# OR
yarn installfor installing the dependencies. At the end type
npm run serve
# OR
yarn servefor running the application.
You can create a optimized build version of this repository running:
npm run build
# OR
yarn buildOr you can see a deployed version on Netlify at this URL: https://fervent-brown-eb1282.netlify.com/#/
All Vuex's configurations are inside the store.js file.
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
}
})Every .js file inside the store directory is transformed as a namespaced module (index being the root module).
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import todos from './modules/todos'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
todos
},
strict: true // strict mode prevents the state changing outside the Vuex store
})store/todo.js
export default {
state: {
todos: [
{
name: 'Learn Vue',
done: true
},
{
name: 'Learn Vuex',
done: true
},
{
name: 'Chill with Vuex',
done: false
},
{
name: 'Improve the knowledge of Vuex',
done: false
},
{
name: 'Chill with Vuex again',
done: false
}
]
},
getters: {
getNumberTodos(state) {
return state.todos.length
},
getNumberDoneTodos(state) {
return state.todos.filter((todos) => todos.done == true).length
},
getNumberUndoneTodos(state) {
return state.todos.filter((todos) => todos.done == false).length
}
},
mutations: {
mutate_todo_done(state, payload) {
state.todos[payload].done = !state.todos[payload].done
},
mutate_with_new_todo(state, payload) {
state.todos.push(payload)
}
},
actions: {
changeDone({ commit }, index) {
commit('mutate_todo_done', index)
},
addNewTodo({ commit }, newTodo) {
commit('mutate_with_new_todo', newTodo)
}
}
}- State: this object contains all the application states, in this case our state is the array of objects
todos. - Getters: they are derived state based on store state, like the
getNumberTodos()function that compute the current numeber of todos in the array. - Mutations: are functions for changing states' values. For example the mutation
mutate_todo_donechange the value of the fielddoneinside the state. - Actions: they are similar to mutations but instead of mutating the state, actions trigger mutations, like the
changeDonethat commits themutate_todo_donepassing the index number of the todo.
- Generate the project
- Setup Vuex with namespaced mode
- Add aditional packages
- Add
todo.jsmodule - Add state
- Add getters
- Add mutations
- Add actions
- Add page and components Todo
- API
- Fetch remote data from API
- Add them to the store
- Add state
- Add getters
- Add mutations
- Add actions
- Vue.js - A Javascript framework
- Vuex - A state management pattern library for Vue.js applications
- Bootstrap Vue - Bootstrap components for Vue.js
Pull Requests for adding features ⇄ and ★ are welcome