In this section, we will guide you through the process of creating your first Ionic app. By the end of this lesson, you will have a basic Ionic application running on your local development environment.
Prerequisites
Before you start, ensure you have the following installed:
- Node.js and npm (Node Package Manager)
- Ionic CLI
If you haven't set up your development environment yet, refer to the previous section: Setting Up the Development Environment.
Step-by-Step Guide
- Install Ionic CLI
First, you need to install the Ionic CLI globally on your machine. Open your terminal and run the following command:
- Create a New Ionic Project
Once the Ionic CLI is installed, you can create a new Ionic project. Run the following command in your terminal:
myFirstApp
is the name of your project. You can choose any name you prefer.blank
is the template for your project. Ionic provides several templates liketabs
,sidemenu
, etc. For this tutorial, we will use theblank
template.
- Navigate to Your Project Directory
After the project is created, navigate to the project directory:
- Serve the Application
To see your app in action, you need to serve it. Run the following command:
This command will start a local development server and open your app in the default web browser. You should see a basic Ionic app with a welcome message.
Understanding the Project Structure
Before we dive deeper, let's take a quick look at the project structure. Here are some key directories and files:
src/
: Contains the source code of your application.app/
: Contains the main app module and component.assets/
: Contains static assets like images and icons.environments/
: Contains environment-specific configuration files.index.html
: The main HTML file.main.ts
: The main entry point of the application.
www/
: Contains the built files for deployment.ionic.config.json
: Configuration file for the Ionic CLI.package.json
: Contains metadata about the project and its dependencies.
Practical Example
Let's add a simple button to our app and display an alert when the button is clicked.
- Open
src/app/app.component.html
and replace its content with the following:
<ion-header> <ion-toolbar> <ion-title> My First Ionic App </ion-title> </ion-toolbar> </ion-header> <ion-content> <ion-button (click)="showAlert()">Click Me!</ion-button> </ion-content>
- Open
src/app/app.component.ts
and add theshowAlert
method:
import { Component } from '@angular/core'; import { AlertController } from '@ionic/angular'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.scss'], }) export class AppComponent { constructor(private alertController: AlertController) {} async showAlert() { const alert = await this.alertController.create({ header: 'Alert', message: 'Button Clicked!', buttons: ['OK'], }); await alert.present(); } }
Explanation
- HTML: We added an Ionic button with a click event that calls the
showAlert
method. - TypeScript: We imported
AlertController
from@ionic/angular
and used it to create and present an alert when the button is clicked.
Running the App
Save the changes and go back to your terminal. If the app is still running, it should automatically reload and reflect the changes. Click the button, and you should see an alert with the message "Button Clicked!".
Summary
In this lesson, you learned how to:
- Install the Ionic CLI.
- Create a new Ionic project.
- Serve the application.
- Understand the basic project structure.
- Add a simple button and display an alert.
In the next section, we will dive deeper into the project structure and understand the role of each file and directory.
Ionic Development Course
Module 1: Introduction to Ionic
- What is Ionic?
- Setting Up the Development Environment
- Creating Your First Ionic App
- Understanding the Project Structure
- Running and Debugging Your App
Module 2: Basic Components and Navigation
- Ionic Components Overview
- Using Ionic Buttons and Icons
- Creating and Using Pages
- Navigation and Routing
- Tabs and Side Menus
Module 3: Styling and Theming
- Introduction to Ionic Styling
- Using CSS and SCSS in Ionic
- Theming Your Ionic App
- Responsive Design in Ionic
- Customizing Ionic Components
Module 4: Working with Data
- Introduction to Data Binding
- Using Angular Services
- HTTP Requests and APIs
- Storing Data Locally
- Using Ionic Storage
Module 5: Advanced Components and Features
- Using Ionic Forms
- Validation and Error Handling
- Using Ionic Native and Cordova Plugins
- Accessing Device Features
- Push Notifications
Module 6: Testing and Deployment
- Unit Testing in Ionic
- End-to-End Testing
- Building for Production
- Deploying to App Stores
- Continuous Integration and Delivery