Ionic & Angular With Firebase CRUD Operations: This step-by-step example will explain how to make a mobile CRUD operations app in Ionic using Google Firebase NoSQL real-time database.
CRUD, which stands for Create, Read, Update, and Delete, is a fundamental building block for every web and mobile app.
It is called CRUD. The word "create" means to add or make new data objects in the database.
Reading means getting data objects from the database and putting them on the view. Update means changing or updating the data, and delete means removing the data object from the database.
This Ionic CRUD tutorial will help people just starting in application development by showing them how to deal with Ionic Firebase CRUD in the best way possible.
In this Ionic Firebase CRUD example, we'll show you how to use Firestore to build CRUD operations for native mobile apps using the Ionic and Angular frameworks.
Ionic 6 & Angular CRUD Operations With Firebase
We'll make an Ionic to-do app, starting with setting up an Ionic and Angular environment, adding firestore to Ionic, and connecting with Ionic and Firebase.
Our Ionic Angular ToDo App will be a simple app with an easy-to-use interface that helps us keep track of our daily tasks.
Users can create tasks, list all tasks, change an existing lesson, or delete a current job. Below are the steps you need to take to put together the app.
- Step 1: Creating A New Ionic Project
- Step 2: Create Firebase Project
- Step 3: Install Firebase & AngularFire Packages
- Step 4: Change Environment Files
- Step 5: Change Routing Module
- Step 6: Add Navigation Routes
- Step 7: Add Angular Service
- Step 8: Create
- Step 9: Read & Delete
- Step 10: Update
- Step 11: Test Ionic App
Creating A New Ionic Project
In the first step, you have to install Ionic CLI, which lets you start ionic development on your system.
Now, you begin the installation of the Ionic Angular app:
Next, head over to the app folder:
To make the Todo CRUD app, we need to make a few pages. To make the new pages, run the following commands from the terminal. We don’t need the Home page, so delete the default Home page component.
Create Firebase Project
In this step, we'll learn how to make a new Firebase project to get the Firebase configuration keys. Use it with the AngularFire package to connect with Ionic and Firestore.
You must go to the Firebase website and open the Firebase console.
Next, give your project a name in the "Create a project" box.
After that, you need to add Firebase to your app, so click on the "Web icon" to get started.
Then, add Firebase to your web app's screen manifests. You'll need to give the app a name and click "Register App."
Simply copy the Firebase SDK keys & save it somewhere. You will need these configuration keys soon.
Also, click on Firestore and create a Cloud Firestore database for real-time updates, powerful queries, and automatic scaling. "Test mode" should be chosen for the demo.
Install Firebase & AngularFire Packages
Now, you have to add the Firebase & AngularFire packages to the Ionic app and then run the following command:
Change Environment Files
Let's add the Firebase configuration keys to the angular environment files. Add the Firebase keys to the environment.ts and environment.prod.ts files one after the other.
Change App Module
Now that we have added the AngularFire Modules & Environment variable to the main app module class, Ionic can connect to the Firebase database.
Update app.module.ts file:
Change Routing Module
To handle CRUD operations, you have to make small changes to the app routing file:
Update the app/app-routing.ts file:
Add Navigation Routes
You need to define tab routes to move around in the crud todo app, so open the app.component.html file and change the code below.
Create Angular Service
Then, create an Angular service to make a reusable part that handles the custom methods for CRUD Operations.
Declare the TODO class to define the task's schema, import AngularFirestore, and inject it into the page's constructor. It gives you access to the Firestore methods, and it's easy to make your methods for CRUD operations.
Update services/crud.service.ts file:
Create
You can use reactive forms to work with form components. To do this, add the ReactiveFormsModule to the page-specific module file. So, add the following code to the file create-todo.module.ts:
Make a simple form and add two form controls with title and description fields.
Update create-todo.page.html file:
The create() method stores the to-do task in the Firestore database. Then, use the router API to go to the to-do list page.
Update create-todo.page.ts file:
Read & Delete
In this step, we'll get the list of tasks and show you how to delete the task object from the Firestore. Use the ion-list UI component to show the ionic data list, and add a button bind remove() function with a click event to get rid of the data object.
Update todo-list.page.html file:
Update todo-list.page.ts file:
Update
In this last example, you'll see how to use the activated route API and the custom update() method to get a single task object from the cloud firestore database.
Register the reactive forms module in the update-todo.module.ts file one more time:
Update update-todo.page.html file:
Update update-todo.page.ts file:
Test The Ionic App
Finally, we're ready to test our crud mobile app, so go to the command prompt and follow the instructions:
First, choose the platform where you want the app to run:
Then, make the build that can be run:
Run the app on the device in the end:
Conclusion
So this is it for Ionic & Firestore CRUD operations tutorial. Using the Firebase NoSQL database, we built an excellent Todo CRUD application in Ionic Angular for the native mobile device.
You can comment below for any query or if you need any help. If you find our article helpful, subscribe to Us On YouTube for more.