|
| 1 | +--- |
| 2 | +title: Ionic Angular Quickstart |
| 3 | +sidebar_label: Quickstart |
| 4 | +--- |
| 5 | + |
| 6 | +<head> |
| 7 | + <title>Angular QuickStart Global Component for Generating Ionic Angular Apps</title> |
| 8 | + <meta |
| 9 | + name="description" |
| 10 | + content="Our QuickStart guide covers the basics of both Angular and Ionic Framework global components to get apps up and running. Read how to easily generate Ionic Angular apps." |
| 11 | + /> |
| 12 | +</head> |
| 13 | + |
| 14 | +import DocsCard from '@components/global/DocsCard'; |
| 15 | +import DocsCards from '@components/global/DocsCards'; |
| 16 | + |
| 17 | +Welcome! This guide will walk you through creating your first Ionic Angular app, step by step. You'll learn how to set up your development environment, generate a new project, explore the project structure, and add your first UI components and navigation. |
| 18 | + |
| 19 | +If you're looking for a high-level overview of what Ionic Angular is and how it fits into the Angular ecosystem, see the [Ionic Angular Overview](overview). |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +Before you begin, make sure you have [Node.js](https://nodejs.org/) (which includes npm) installed on your machine. |
| 24 | +You can check by running: |
| 25 | + |
| 26 | +```shell |
| 27 | +node -v |
| 28 | +npm -v |
| 29 | +``` |
| 30 | + |
| 31 | +If you don't have Node.js, [download it here](https://nodejs.org/en/download). |
| 32 | + |
| 33 | +## Create a Project with the Ionic CLI |
| 34 | + |
| 35 | +First, install the latest [Ionic CLI](../cli): |
| 36 | + |
| 37 | +```shell |
| 38 | +npm install -g @ionic/cli |
| 39 | +``` |
| 40 | + |
| 41 | +Then, run the following commands to create and run a new project: |
| 42 | + |
| 43 | +```shell |
| 44 | +ionic start myApp blank --type angular |
| 45 | + |
| 46 | +cd myApp |
| 47 | +ionic serve |
| 48 | +``` |
| 49 | + |
| 50 | +At the first prompt, choose `Standalone`. |
| 51 | + |
| 52 | +After running `ionic serve`, your project will open in the browser. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +## Explore the Project Structure |
| 57 | + |
| 58 | +Your new app's `src/app` directory will look like this: |
| 59 | + |
| 60 | +```shell |
| 61 | +├── app.component.html |
| 62 | +├── app.component.scss |
| 63 | +├── app.component.ts |
| 64 | +├── app.routes.ts |
| 65 | +└── home/ |
| 66 | + ├── home.page.html |
| 67 | + ├── home.page.scss |
| 68 | + ├── home.page.spec.ts |
| 69 | + └── home.page.ts |
| 70 | +``` |
| 71 | + |
| 72 | +Let's walk through these files to understand the app's structure. |
| 73 | + |
| 74 | +## View the App Component |
| 75 | + |
| 76 | +The root of your app is defined in `app.component.ts`: |
| 77 | + |
| 78 | +```ts |
| 79 | +import { Component } from '@angular/core'; |
| 80 | +import { IonApp, IonRouterOutlet } from '@ionic/angular/standalone'; |
| 81 | + |
| 82 | +@Component({ |
| 83 | + selector: 'app-root', |
| 84 | + templateUrl: 'app.component.html', |
| 85 | + imports: [IonApp, IonRouterOutlet], |
| 86 | +}) |
| 87 | +export class AppComponent { |
| 88 | + constructor() {} |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +And its template in `app.component.html`: |
| 93 | + |
| 94 | +```html |
| 95 | +<ion-app> |
| 96 | + <ion-router-outlet></ion-router-outlet> |
| 97 | +</ion-app> |
| 98 | +``` |
| 99 | + |
| 100 | +This sets up the root of your application, using Ionic's `ion-app` and `ion-router-outlet` components. The router outlet is where your pages will be displayed. |
| 101 | + |
| 102 | +## View Routes |
| 103 | + |
| 104 | +Routes are defined in `app.routes.ts`: |
| 105 | + |
| 106 | +```ts |
| 107 | +import { Routes } from '@angular/router'; |
| 108 | + |
| 109 | +export const routes: Routes = [ |
| 110 | + { |
| 111 | + path: 'home', |
| 112 | + loadComponent: () => import('./home/home.page').then((m) => m.HomePage), |
| 113 | + }, |
| 114 | + { |
| 115 | + path: '', |
| 116 | + redirectTo: 'home', |
| 117 | + pathMatch: 'full', |
| 118 | + }, |
| 119 | +]; |
| 120 | +``` |
| 121 | + |
| 122 | +When you visit the root URL (`/`), the `HomePage` component will be loaded. |
| 123 | + |
| 124 | +## View the Home Page |
| 125 | + |
| 126 | +The Home page component, defined in `home.page.ts`, imports the Ionic components it uses: |
| 127 | + |
| 128 | +```ts |
| 129 | +import { Component } from '@angular/core'; |
| 130 | +import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone'; |
| 131 | + |
| 132 | +@Component({ |
| 133 | + selector: 'app-home', |
| 134 | + templateUrl: 'home.page.html', |
| 135 | + styleUrls: ['home.page.scss'], |
| 136 | + imports: [IonHeader, IonToolbar, IonTitle, IonContent], |
| 137 | +}) |
| 138 | +export class HomePage { |
| 139 | + constructor() {} |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +And the template, in the `home.page.html` file, uses those components: |
| 144 | + |
| 145 | +```html |
| 146 | +<ion-header [translucent]="true"> |
| 147 | + <ion-toolbar> |
| 148 | + <ion-title> Blank </ion-title> |
| 149 | + </ion-toolbar> |
| 150 | +</ion-header> |
| 151 | + |
| 152 | +<ion-content [fullscreen]="true"> |
| 153 | + <ion-header collapse="condense"> |
| 154 | + <ion-toolbar> |
| 155 | + <ion-title size="large">Blank</ion-title> |
| 156 | + </ion-toolbar> |
| 157 | + </ion-header> |
| 158 | + |
| 159 | + <div id="container"> |
| 160 | + <strong>Ready to create an app?</strong> |
| 161 | + <p> |
| 162 | + Start with Ionic |
| 163 | + <a target="_blank" rel="noopener noreferrer" href="https://ionicframework.com/docs/components">UI Components</a> |
| 164 | + </p> |
| 165 | + </div> |
| 166 | +</ion-content> |
| 167 | +``` |
| 168 | + |
| 169 | +This creates a page with a header and scrollable content area. The second header shows a [collapsible large title](/docs/api/title#collapsible-large-titles) that displays when at the top of the content, then condenses to show the smaller title in the first header when scrolling down. |
| 170 | + |
| 171 | +:::tip Learn More |
| 172 | +For detailed information about Ionic layout components, see the [Header](/docs/api/header), [Toolbar](/docs/api/toolbar), [Title](/docs/api/title), and [Content](/docs/api/content) documentation. |
| 173 | +::: |
| 174 | + |
| 175 | +## Add an Ionic Component |
| 176 | + |
| 177 | +You can enhance your Home page with more Ionic UI components. For example, add a [Button](/docs/api/button) at the end of the `ion-content`: |
| 178 | + |
| 179 | +```html |
| 180 | +<ion-content> |
| 181 | + <!-- existing content --> |
| 182 | + |
| 183 | + <ion-button>Navigate</ion-button> |
| 184 | +</ion-content> |
| 185 | +``` |
| 186 | + |
| 187 | +Then, import the `IonButton` component in `home.page.ts`: |
| 188 | + |
| 189 | +```ts |
| 190 | +import { IonButton, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular/standalone'; |
| 191 | + |
| 192 | +@Component({ |
| 193 | + // ...existing config... |
| 194 | + imports: [IonButton, IonContent, IonHeader, IonTitle, IonToolbar], |
| 195 | +}) |
| 196 | +``` |
| 197 | + |
| 198 | +## Add a New Page |
| 199 | + |
| 200 | +To add a new page, generate it with the CLI: |
| 201 | + |
| 202 | +```shell |
| 203 | +ionic generate page new |
| 204 | +``` |
| 205 | + |
| 206 | +A route will be automatically added to `app.routes.ts`. |
| 207 | + |
| 208 | +In your `new.page.html`, you can add a [Back Button](/docs/api/back-button) to the [Toolbar](/docs/api/toolbar): |
| 209 | + |
| 210 | +```html |
| 211 | +<ion-header [translucent]="true"> |
| 212 | + <ion-toolbar> |
| 213 | + <ion-buttons slot="start"> |
| 214 | + <ion-back-button defaultHref="/"></ion-back-button> |
| 215 | + </ion-buttons> |
| 216 | + <ion-title>new</ion-title> |
| 217 | + </ion-toolbar> |
| 218 | +</ion-header> |
| 219 | +``` |
| 220 | + |
| 221 | +And import `IonBackButton` and `IonButtons` in `new.page.ts`: |
| 222 | + |
| 223 | +```ts |
| 224 | +import { IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular/standalone'; |
| 225 | + |
| 226 | +@Component({ |
| 227 | + // ...existing config... |
| 228 | + imports: [IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar], |
| 229 | +}) |
| 230 | +``` |
| 231 | + |
| 232 | +The `ion-back-button` will automatically handle navigation back to the previous page, or to `/` if there is no history. |
| 233 | + |
| 234 | +## Navigate to the New Page |
| 235 | + |
| 236 | +To navigate to the new page, update the button in `home.page.html`: |
| 237 | + |
| 238 | +```html |
| 239 | +<ion-button [routerLink]="['/new']">Navigate</ion-button> |
| 240 | +``` |
| 241 | + |
| 242 | +Then, import `RouterLink` in `home.page.ts`: |
| 243 | + |
| 244 | +```ts |
| 245 | +import { RouterLink } from '@angular/router'; |
| 246 | + |
| 247 | +@Component({ |
| 248 | + // ...existing config... |
| 249 | + imports: [IonButton, IonContent, IonHeader, IonTitle, IonToolbar, RouterLink], |
| 250 | +}) |
| 251 | +``` |
| 252 | + |
| 253 | +:::info |
| 254 | +Navigating can also be performed using Angular's Router service. See the [Angular Navigation documentation](/docs/angular/navigation#navigating-to-different-routes) for more information. |
| 255 | +::: |
| 256 | + |
| 257 | +## Add Icons to the New Page |
| 258 | + |
| 259 | +Ionic Angular comes with [Ionicons](https://ionic.io/ionicons/) pre-installed. You can use any icon by setting the `name` property on the `ion-icon` component. Add the following icons to `new.page.html`: |
| 260 | + |
| 261 | +```html |
| 262 | +<ion-content> |
| 263 | + <!-- existing content --> |
| 264 | + |
| 265 | + <ion-icon name="heart"></ion-icon> |
| 266 | + <ion-icon name="logo-ionic"></ion-icon> |
| 267 | +</ion-content> |
| 268 | +``` |
| 269 | + |
| 270 | +You'll also need to import and register these icons in `new.page.ts`: |
| 271 | + |
| 272 | +```ts |
| 273 | +// ...existing imports... |
| 274 | +import { IonBackButton, IonButtons, IonContent, IonHeader, IonIcon, IonTitle, IonToolbar } from '@ionic/angular/standalone'; |
| 275 | +import { addIcons } from 'ionicons'; |
| 276 | +import { heart, logoIonic } from 'ionicons/icons'; |
| 277 | + |
| 278 | +@Component({ |
| 279 | + // ...existing config... |
| 280 | + imports: [IonBackButton, IonButtons, IonContent, IonHeader, IonIcon, IonTitle, IonToolbar], |
| 281 | +}) |
| 282 | +``` |
| 283 | + |
| 284 | +Then, update the constructor of the page to use `addIcons`: |
| 285 | + |
| 286 | +```ts |
| 287 | +export class NewPage implements OnInit { |
| 288 | + constructor() { |
| 289 | + addIcons({ heart, logoIonic }); |
| 290 | + } |
| 291 | +} |
| 292 | +``` |
| 293 | + |
| 294 | +Alternatively, you can register icons in `app.component.ts` to use them throughout your app. |
| 295 | + |
| 296 | +For more information, see the [Icon documentation](/docs/api/icon) and the [Ionicons documentation](https://ionic.io/ionicons/). |
| 297 | + |
| 298 | +## Run on a Device |
| 299 | + |
| 300 | +Ionic's components work everywhere: iOS, Android, and PWAs. To deploy to mobile, use [Capacitor](https://capacitorjs.com): |
| 301 | + |
| 302 | +```shell |
| 303 | +ionic build |
| 304 | +ionic cap add ios |
| 305 | +ionic cap add android |
| 306 | +``` |
| 307 | + |
| 308 | +Open the native projects in their IDEs: |
| 309 | + |
| 310 | +```shell |
| 311 | +ionic cap open ios |
| 312 | +ionic cap open android |
| 313 | +``` |
| 314 | + |
| 315 | +See [Capacitor's Getting Started guide](https://capacitorjs.com/docs/getting-started/with-ionic) for more. |
| 316 | + |
| 317 | +## Explore More |
| 318 | + |
| 319 | +This guide covered the basics of creating an Ionic Angular app, adding navigation, and introducing Capacitor for native builds. To dive deeper, check out: |
| 320 | + |
| 321 | +<DocsCards> |
| 322 | + |
| 323 | +<DocsCard header="Build Your First App" href="your-first-app" icon="/icons/component-content-icon.png"> |
| 324 | + <p>Build a real Photo Gallery app with Ionic Angular and native device features.</p> |
| 325 | +</DocsCard> |
| 326 | + |
| 327 | +<DocsCard header="Angular Documentation" href="https://angular.dev/overview" icon="/icons/logo-angular-icon.png"> |
| 328 | + <p>Learn more about Angular's core concepts, tools, and best practices from the official Angular documentation.</p> |
| 329 | +</DocsCard> |
| 330 | + |
| 331 | +<DocsCard header="Navigation" href="navigation" icon="/icons/component-navigation-icon.png"> |
| 332 | + <p>Discover how to handle routing and navigation in Ionic Angular apps using the Angular Router.</p> |
| 333 | +</DocsCard> |
| 334 | + |
| 335 | +<DocsCard header="Components" href="/docs/components" icon="/icons/guide-components-icon.png"> |
| 336 | + <p>Explore Ionic's rich library of UI components for building beautiful apps.</p> |
| 337 | +</DocsCard> |
| 338 | + |
| 339 | +<DocsCard header="Theming" href="/docs/theming/basics" icon="/icons/guide-theming-icon.png"> |
| 340 | + <p>Learn how to customize the look and feel of your app with Ionic's powerful theming system.</p> |
| 341 | +</DocsCard> |
| 342 | + |
| 343 | +<DocsCard header="Capacitor Documentation" href="https://capacitorjs.com/docs/" icon="/icons/guide-capacitor-icon.png"> |
| 344 | + <p>Explore how to access native device features and deploy your app to iOS, Android, and the web with Capacitor.</p> |
| 345 | +</DocsCard> |
| 346 | + |
| 347 | +</DocsCards> |
0 commit comments