Vue.js Vuex 2.0 TODO CRUD List App in Browser Using Javascript Full Project For Beginners

Vue.js Vuex 2.0 TODO CRUD List App in Browser Using Javascript Full Project For Beginners
  • Post author:
  • Post category:Vue
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be building a vuex 2.0 todo crud list app using vue.js in browser using javascript. All the full source code of the application is shown below.

 

 

Screenshot

 

 

The screenshots of the vuex vue.js todo app are as shown below

 

 

 

Live Demo

 

 

Vue.js Vuex TODO App

 

 

Features

 

 

This app supports creating new todos app

 

This app supports updating todos app

 

This app supports deletes todos as well

 

This app supports displaying list of todos

 

 

Get Started

 

 

In order to get started just create a brand new vue.js project and then your directory structure will look as shown below

 

 

So now firstly we will create the App.vue file and copy paste the below code

 

App.vue

 

<template>
  <div id="app"><New /><Todos /><Status /></div>
</template>

<script>
import Status from "./components/status";
import Todos from "./components/todos";
import New from "./components/new";

export default {
  name: "App",
  components: {
    New,
    Todos,
    Status
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

 

 

Now create main.js inside the root directory and copy paste the below code

 

main.js

 

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import { store } from './store/store';

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
    el: '#app',
    components: { App },
    template: '<App/>',
    store
});

 

See also  Vue.js FusionChart.js Library Example to Draw Charts Using vue-fusioncharts Component Full Example For Beginners

 

Adding Vuex Store

 

 

Now we will be adding vuex inside our vue.js app so just create a store folder inside the root folder and then create store.js file and copy paste the below code

 

store/store.js

 

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        count: 0,
        todos: [
            { id: 1, text: 'Collect packages', done: false },
            { id: 2, text: 'Workout', done: false },
            { id: 3, text: 'Read one chapter', done: false },
            { id: 4, text: 'Buy socks', done: true }
        ]
    },
    mutations: {
        increment: state => state.count++,
        decrement: state => state.count--,
        missionCompleted: function(state, todoId) {
            let todo = store.getters.thisTodo(todoId);
            todo.done = !todo.done;
        },
        addTodo: function(state, todoText) {
            state.todos.push({
                id: state.todos.slice(-1)[0].id + 1,
                text: todoText,
                done: false
            });
            console.log(state.todos);
        },
        deleteTodo: function(state, todoId) {
            let todoIndex = state.todos.indexOf(
                store.getters.thisTodo(todoId)
            );
            state.todos.splice(todoIndex, 1);
            delete state.todos[todoIndex].text;
            console.log(state.todos[todoIndex]);
        }
    },
    getters: {
        thisTodo: state => todoId => {
            return state.todos.find(todo => todo.id === todoId);
        },
        doneTodos: state => {
            return state.todos.filter(todo => todo.done);
        },
        doneTodosCount: (state, getters) => {
            return getters.doneTodos.length;
        },
        activeTodos: state => {
            return state.todos.filter(todo => !todo.done);
        },
        activeTodosCount: (state, getters) => {
            return getters.activeTodos.length;
        }
    },
    actions: {
        incrementAsync({ commit }) {
            console.log('go');
            setTimeout(() => {
                commit('increment');
            }, 1000);
        }
    }
});

 

 

Creating Vue Components

 

 

Now we will be creating the vue.js components for our app firstly we will create the components folder and then inside it create a new.vue file and copy paste the below code

See also  Build a Vuetify Currency Converter in Vue.js Deployed to Heroku Using Axios Library in Browser Using HTML5 & Javascript Full Project For Beginnerss

 

new.vue

 

<template>
  <div class="new">
    <input
      type="text"
      id="new-todo"
      placeholder="to do something"
      v-model="newTodoInput"
      v-on:keydown.enter="addTodo(newTodoInput);"
    >
    <button class="add-new-btn" @click="addTodo(newTodoInput);">Add</button>
  </div>
</template>

<script>
export default {
  name: "New",
  data() {
    return {
      newTodoInput: ""
    };
  },
  methods: {
    addTodo(newTodo) {
      this.$store.commit("addTodo", newTodo);
      this.newTodoInput = "";
    }
  }
};
</script>

<style lang="sass" scoped>
.new
  width: 100%
  font-size: 18px

#new-todo
  padding: 5px 10px
  line-height: 1
  &:focus
    outline: none

.add-new-btn
  padding: 6px 10px
  border: 1px solid #dadadc
  background-color: #efefef
  &:hover
    cursor: pointer
</style>

 

 

Now create a status.vue file inside the same folder and copy paste the below code

 

status.vue

 

<template>
  <div class="new">
    <input
      type="text"
      id="new-todo"
      placeholder="to do something"
      v-model="newTodoInput"
      v-on:keydown.enter="addTodo(newTodoInput);"
    >
    <button class="add-new-btn" @click="addTodo(newTodoInput);">Add</button>
  </div>
</template>

<script>
export default {
  name: "New",
  data() {
    return {
      newTodoInput: ""
    };
  },
  methods: {
    addTodo(newTodo) {
      this.$store.commit("addTodo", newTodo);
      this.newTodoInput = "";
    }
  }
};
</script>

<style lang="sass" scoped>
.new
  width: 100%
  font-size: 18px

#new-todo
  padding: 5px 10px
  line-height: 1
  &:focus
    outline: none

.add-new-btn
  padding: 6px 10px
  border: 1px solid #dadadc
  background-color: #efefef
  &:hover
    cursor: pointer
</style>

 

 

Now create a todos.vue file and copy paste the below code

 

todos.vue

 

<template>
  <div class="new">
    <input
      type="text"
      id="new-todo"
      placeholder="to do something"
      v-model="newTodoInput"
      v-on:keydown.enter="addTodo(newTodoInput);"
    >
    <button class="add-new-btn" @click="addTodo(newTodoInput);">Add</button>
  </div>
</template>

<script>
export default {
  name: "New",
  data() {
    return {
      newTodoInput: ""
    };
  },
  methods: {
    addTodo(newTodo) {
      this.$store.commit("addTodo", newTodo);
      this.newTodoInput = "";
    }
  }
};
</script>

<style lang="sass" scoped>
.new
  width: 100%
  font-size: 18px

#new-todo
  padding: 5px 10px
  line-height: 1
  &:focus
    outline: none

.add-new-btn
  padding: 6px 10px
  border: 1px solid #dadadc
  background-color: #efefef
  &:hover
    cursor: pointer
</style>

Leave a Reply