Create ToDo List with Vue.js

Syed Muhammad Ali Raza - Jul 9 '23 - - Dev Community

Introduction:

A to-do list app is a simple yet effective way to track tasks and organize your daily workflow. In this tutorial, we will learn how to build a to-do list application using Vue.js, a popular JavaScript framework for building user interfaces.

Prerequisites:

you should have a basic understanding of HTML, CSS, and JavaScript. Also, make sure you have Vue.js installed on your device.

Start:
Let's start by setting up the basic structure of our project. Create a new HTML file and add the Vue.js library using the Script tag:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue.js To-Do List</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
</head>
<body>
  <div id="app">
    <!-- Your code goes here -->
  </div>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Next, let's define the structure of our task list.
Add a login area and a button to add new tasks to the application wall:

<div id="app">
  <input type="text" v-model="newTask" placeholder="Add a new task">
  <button @click="addTask">Add</button>
</div>

Enter fullscreen mode Exit fullscreen mode

We used the v-model to bind the value of the input field to the newTask data property in Vue. When the button is clicked, it will call the addTask method.

Data constructs and methods:

Now, let's define the Vue instance and configure the required data and methods. Add this script tag after the HTML code:

<script>
  new Vue({
    el: '#app',
    data: {
      newTask: '',
      tasks: []
    },
    methods: {
      addTask() {
        if (this.newTask.trim() !== '') {
          this.tasks.push(this.newTask);
          this.newTask = '';
        }
      }
    }
  });
</script>

Enter fullscreen mode Exit fullscreen mode

Here, we have defined the newTask data property to store the input field values ​​and an empty task array to hold the task list. The AddTask method checks whether newTask is empty, adds it to the task collection, and resets the input field.

Display function:

Now that we can add tasks to the list, let's display them on the screen. Change the HTML code like this:

<div id="app">
  <input type="text" v-model="newTask" placeholder="Add a new task">
  <button @click="addTask">Add</button>
 <ul>
    <li v-for="task in tasks" :key="task">{{ task }}</li>
  </ul>
</div>

Enter fullscreen mode Exit fullscreen mode

In the code, we use the v-directive to iterate over a set of tasks and represent each task as a

  • element. The main attribute helps Vue track elements efficiently.

    Make a to-do list:

    To make our task list visually appealing, add basic styling using a CSS framework such as CSS or Bulma. Add the following CSS code to the

    section of your HTML file:
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
    
    

    This will import Bulma's CSS framework. You can customize the style you want.

    The results:
    Congratulations!. You have successfully created a task list application using You have learned how to create a basic structure, add tasks to the list, and display them on the screen.
    With more research, you can improve the application by adding features such as completing tasks, breaking down tasks, or storing tasks in a database.

  • . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .