Vue.js tutorials with CRUD operations
In this tutorial, we'll walk through creating a simple CRUD (Create, Read, Update, Delete) application using Vue.js. We'll manage the data in the frontend (without a backend or database) and use localStorage to persist data even after the page is refreshed. Vue.js is a progressive JavaScript framework that is easy to integrate into projects.
Steps:
- Set up the Vue.js Project
- Create the components for CRUD operations
- Use localStorage for data persistence
- Test the application
1. Set Up the Vue.js Project
First, let's create a basic Vue.js app. If you don't have Vue CLI installed, you can install it using npm:
Now, create a new Vue project:
Navigate into your project directory:
To serve the application, run:
This will start a local development server at http://localhost:8080/
.
2. Create the Components for CRUD Operations
We will create a Todo App where users can:
- Create new todos
- Read (view) the list of todos
- Update an existing todo
- Delete a todo
2.1. Modify the App.vue
First, we'll edit the App.vue
file to include the necessary components and functionality.
Explanation:
HTML Form:
- The form allows the user to input a new todo. The
v-model
directive binds the input field to thenewTodo
data property. - The form also conditionally renders the button text depending on whether it's in "edit" mode or "add" mode.
- The form allows the user to input a new todo. The
Todos List:
- We loop through the
todos
array and display each todo item in an unordered list. - Each todo has buttons for editing, deleting, and toggling its completion status.
- We loop through the
Methods:
addTodo
: Adds a new todo to the list and stores it inlocalStorage
.editTodo
: Sets the input field with the todo's text and switches to edit mode.updateTodo
: Updates an existing todo and switches back to add mode.deleteTodo
: Deletes the selected todo.toggleCompletion
: Marks a todo as completed or incomplete.saveTodos
: Saves thetodos
array tolocalStorage
for persistence across page refreshes.
Styling:
- Simple CSS for basic styling. The
.completed
class is used to apply a line-through effect on completed todos.
- Simple CSS for basic styling. The
3. Use localStorage for Data Persistence
By using localStorage
, the todos will persist even when the user refreshes the page or closes and reopens the browser. The todos
are stored as a JSON string in localStorage
.
In the data
property of our Vue component, we initialize the todos
array by retrieving the stored value from localStorage
. If there is no saved todos, we default to an empty array.
4. Testing the Application
To test the application:
Run your Vue app using the following command:
Open the app in your browser at
http://localhost:8080/
.You should see:
- A form to enter new todos.
- A list of todos with options to edit, delete, and mark as completed.
Test the CRUD functionality:
- Create: Add a new todo and see it added to the list.
- Read: View the list of todos.
- Update: Click the "Edit" button, change the text, and update it.
- Delete: Click the "Delete" button to remove a todo.
- Toggle Completion: Click the "Complete" button to mark the todo as completed.
Conclusion
In this tutorial, we built a simple Vue.js CRUD app where you can:
- Create new todos.
- Read (view) the list of todos.
- Update a todo.
- Delete a todo.
- Use localStorage to persist the data.
This app demonstrates the power of Vue.js and how you can easily manage state and user interactions with simple components and methods. You can extend this app by adding more advanced features, like input validation, due dates, or even a backend API.
Comments
Post a Comment