Git Product home page Git Product logo

vuejs-vuex-for-dummies's Introduction

vuejs-vuex-for-dummies

Build Status Netlify Status npm version npm version vue contributions welcome

A Vue.js app with Vuex: a centralized state manager for Vue

Introduction

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. ๐Ÿ˜ƒ

Getting Started

Prerequisites

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

Installing

Clone the repository using Git:

git clone https://github.com/dj0nny/vuejs-state-management-for-beginners.git

Or download the repository here


Run

npm run install
# OR
yarn install

for installing the dependencies. At the end type

npm run serve
# OR
yarn serve

for running the application.

Deploying

You can create a optimized build version of this repository running:

npm run build
# OR
yarn build

Or you can see a deployed version on Netlify at this URL: https://fervent-brown-eb1282.netlify.com/#/

Setup Vuex

Classic Mode

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: {
  }
})

Namespaced Modules

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)
    }
  }
}

Terminology

  • 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_done change the value of the field done inside the state.
  • Actions: they are similar to mutations but instead of mutating the state, actions trigger mutations, like the changeDone that commits the mutate_todo_done passing the index number of the todo.

Next version

  • Generate the project
  • Setup Vuex with namespaced mode
  • Add aditional packages
  • Add todo.js module
  • 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

Built with โค using:

  • Vue.js - A Javascript framework
  • Vuex - A state management pattern library for Vue.js applications
  • Bootstrap Vue - Bootstrap components for Vue.js

Contributing

Pull Requests for adding features โ‡„ and โ˜… are welcome

vuejs-vuex-for-dummies's People

Contributors

dj0nny avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.