Skip to content

Commit cb1c831

Browse files
committed
docs(quickstart): add Call Component Methods section and update paths
1 parent 4f9b716 commit cb1c831

File tree

8 files changed

+573
-41
lines changed

8 files changed

+573
-41
lines changed

docs/angular/quickstart.md

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ Your new app's `src/app` directory will look like this:
6969
└── home.page.ts
7070
```
7171

72+
:::info
73+
All file paths in the examples below are relative to the `src/app/` directory.
74+
:::
75+
7276
Let's walk through these files to understand the app's structure.
7377

7478
## View the App Component
@@ -123,7 +127,7 @@ When you visit the root URL (`/`), the `HomePage` component will be loaded.
123127

124128
## View the Home Page
125129

126-
The Home page component, defined in `home.page.ts`, imports the Ionic components it uses:
130+
The Home page component, defined in `home/home.page.ts`, imports the Ionic components it uses:
127131

128132
```ts
129133
import { Component } from '@angular/core';
@@ -205,7 +209,7 @@ ionic generate page new
205209

206210
A route will be automatically added to `app.routes.ts`.
207211

208-
In your `new.page.html`, you can add a [Back Button](/docs/api/back-button) to the [Toolbar](/docs/api/toolbar):
212+
In your `new/new.page.html`, you can add a [Back Button](/docs/api/back-button) to the [Toolbar](/docs/api/toolbar):
209213

210214
```html
211215
<ion-header [translucent]="true">
@@ -218,7 +222,7 @@ In your `new.page.html`, you can add a [Back Button](/docs/api/back-button) to t
218222
</ion-header>
219223
```
220224

221-
And import `IonBackButton` and `IonButtons` in `new.page.ts`:
225+
And import `IonBackButton` and `IonButtons` in `new/new.page.ts`:
222226

223227
```ts
224228
import { IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular/standalone';
@@ -233,13 +237,13 @@ The `ion-back-button` will automatically handle navigation back to the previous
233237

234238
## Navigate to the New Page
235239

236-
To navigate to the new page, update the button in `home.page.html`:
240+
To navigate to the new page, update the button in `home/home.page.html`:
237241

238242
```html
239243
<ion-button [routerLink]="['/new']">Navigate</ion-button>
240244
```
241245

242-
Then, import `RouterLink` in `home.page.ts`:
246+
Then, import `RouterLink` in `home/home.page.ts`:
243247

244248
```ts
245249
import { RouterLink } from '@angular/router';
@@ -256,7 +260,7 @@ Navigating can also be performed using Angular's Router service. See the [Angula
256260

257261
## Add Icons to the New Page
258262

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`:
263+
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/new.page.html`:
260264

261265
```html
262266
<ion-content>
@@ -267,7 +271,7 @@ Ionic Angular comes with [Ionicons](https://ionic.io/ionicons/) pre-installed. Y
267271
</ion-content>
268272
```
269273

270-
You'll also need to import and register these icons in `new.page.ts`:
274+
You'll also need to import and register these icons in `new/new.page.ts`:
271275

272276
```ts
273277
// ...existing imports...
@@ -288,13 +292,101 @@ export class NewPage implements OnInit {
288292
constructor() {
289293
addIcons({ heart, logoIonic });
290294
}
295+
296+
ngOnInit() {}
291297
}
292298
```
293299

294300
Alternatively, you can register icons in `app.component.ts` to use them throughout your app.
295301

296302
For more information, see the [Icon documentation](/docs/api/icon) and the [Ionicons documentation](https://ionic.io/ionicons/).
297303

304+
## Call Component Methods
305+
306+
Let's add a button that can scroll the content area to the bottom.
307+
308+
Update the `ion-content` in your `new/new.page.html` to include a button and some items after the existing icons:
309+
310+
```html
311+
<ion-content [fullscreen]="true" #content>
312+
<ion-header collapse="condense">
313+
<ion-toolbar>
314+
<ion-title size="large">new</ion-title>
315+
</ion-toolbar>
316+
</ion-header>
317+
318+
<ion-icon name="heart"></ion-icon>
319+
<ion-icon name="logo-ionic"></ion-icon>
320+
321+
<ion-button (click)="scrollToBottom()">Scroll to Bottom</ion-button>
322+
323+
<!-- Add lots of content to make scrolling possible -->
324+
@for (item of items; track $index; let i = $index) {
325+
<ion-item>
326+
<ion-label>Item {{ i + 1 }}</ion-label>
327+
</ion-item>
328+
}
329+
</ion-content>
330+
```
331+
332+
In the component, add the `ViewChild` import, the new component imports and define the `scrollToBottom` function:
333+
334+
```ts
335+
import { Component, OnInit, ViewChild } from '@angular/core';
336+
import {
337+
IonBackButton,
338+
IonButton,
339+
IonButtons,
340+
IonContent,
341+
IonHeader,
342+
IonIcon,
343+
IonItem,
344+
IonLabel,
345+
IonTitle,
346+
IonToolbar,
347+
} from '@ionic/angular/standalone';
348+
import { addIcons } from 'ionicons';
349+
import { heart, logoIonic } from 'ionicons/icons';
350+
351+
@Component({
352+
// ...existing config...
353+
imports: [
354+
IonBackButton,
355+
IonButton,
356+
IonButtons,
357+
IonContent,
358+
IonHeader,
359+
IonIcon,
360+
IonItem,
361+
IonLabel,
362+
IonTitle,
363+
IonToolbar,
364+
],
365+
})
366+
export class NewPage implements OnInit {
367+
@ViewChild(IonContent) content!: IonContent;
368+
369+
items = Array.from({ length: 50 }, (_, i) => i);
370+
371+
constructor() {
372+
addIcons({ heart, logoIonic });
373+
}
374+
375+
ngOnInit() {}
376+
377+
scrollToBottom = () => {
378+
this.content.scrollToBottom(300);
379+
};
380+
}
381+
```
382+
383+
To call methods on Ionic components:
384+
385+
1. Create a `ViewChild` reference for the component
386+
2. Call the method directly on the component instance
387+
388+
You can find available methods for each component in the [Methods](/docs/api/content#methods) section of their API documentation.
389+
298390
## Run on a Device
299391

300392
Ionic's components work everywhere: on iOS, Android, and PWAs. To deploy to mobile, use [Capacitor](https://capacitorjs.com):

docs/react/quickstart.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Your new app's `src` directory will look like this:
6666
   └── Home.tsx
6767
```
6868

69+
:::info
70+
All file paths in the examples below are relative to the `src/` directory.
71+
:::
72+
6973
Let's walk through these files to understand the app's structure.
7074

7175
## View the App Component
@@ -159,7 +163,7 @@ For detailed information about Ionic layout components, see the [Header](/docs/a
159163

160164
## Add an Ionic Component
161165

162-
You can enhance your Home page with more Ionic UI components. For example, import and add a [Button](/docs/api/button) at the end of the `IonContent`:
166+
You can enhance your Home page with more Ionic UI components. For example, import and add a [Button](/docs/api/button) at the end of the `IonContent` in `pages/Home.tsx`:
163167

164168
```tsx
165169
import { IonButton, IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
@@ -187,7 +191,7 @@ export default Home;
187191

188192
## Add a New Page
189193

190-
Create a new page at `src/pages/New.tsx`:
194+
Create a new page at `pages/New.tsx`:
191195

192196
```tsx
193197
import { IonBackButton, IonButtons, IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
@@ -247,7 +251,7 @@ Then, add its route in `IonRouterOutlet`:
247251
</IonRouterOutlet>
248252
```
249253

250-
Once that is done, update the button in `Home.tsx`:
254+
Once that is done, update the button in `pages/Home.tsx`:
251255

252256
```tsx
253257
<IonButton routerLink="/new">Navigate</IonButton>
@@ -261,7 +265,7 @@ Navigating can also be performed programmatically using React Router's `history`
261265

262266
Ionic React comes with [Ionicons](https://ionic.io/ionicons/) pre-installed. You can use any icon by setting the `icon` property of the `IonIcon` component.
263267

264-
Update the imports in `New.tsx` to import `IonIcon` and the `heart` and `logoIonic` icons:
268+
Update the imports in `pages/New.tsx` to import `IonIcon` and the `heart` and `logoIonic` icons:
265269

266270
```tsx
267271
import { IonBackButton, IonButtons, IonContent, IonHeader, IonIcon, IonPage, IonTitle, IonToolbar } from '@ionic/react';
@@ -279,6 +283,59 @@ Note that we are passing the imported SVG reference, **not** the icon name as a
279283

280284
For more information, see the [Icon documentation](/docs/api/icon) and the [Ionicons documentation](https://ionic.io/ionicons/).
281285

286+
## Call Component Methods
287+
288+
Let's add a button that can scroll the content area to the bottom.
289+
290+
Update `pages/New.tsx` to add a `ref` on `IonContent` and a button and some items after the existing icons:
291+
292+
```tsx
293+
<IonContent ref={content}>
294+
<IonIcon icon={heart} />
295+
<IonIcon icon={logoIonic} />
296+
297+
<IonButton onClick={scrollToBottom}>Scroll to Bottom</IonButton>
298+
299+
{/* Add lots of content to make scrolling possible */}
300+
{Array.from({ length: 50 }, (_, i) => (
301+
<IonItem key={i}>
302+
<IonLabel>Item {i + 1}</IonLabel>
303+
</IonItem>
304+
))}
305+
</IonContent>
306+
```
307+
308+
Then, add the imports for the additional components and define the `scrollToBottom` function:
309+
310+
```tsx
311+
import { useRef } from 'react';
312+
import { IonButton, IonBackButton, IonButtons, IonContent, IonHeader, IonIcon, IonItem, IonLabel, IonPage, IonTitle, IonToolbar } from '@ionic/react';
313+
import { heart, logoIonic } from 'ionicons/icons';
314+
315+
const New: React.FC = () => {
316+
const content = useRef<HTMLIonContentElement>(null);
317+
318+
const scrollToBottom = () => {
319+
content.current?.scrollToBottom(300);
320+
};
321+
322+
return (
323+
// ...existing template...
324+
);
325+
};
326+
327+
export default New;
328+
```
329+
330+
To call methods on Ionic components:
331+
332+
1. Create a `ref` for the component
333+
2. Call the method directly on `ref.current`
334+
335+
This pattern is necessary because React refs store the component instance in the `.current` property.
336+
337+
You can find available methods for each component in the [Methods](/docs/api/content#methods) section of their API documentation.
338+
282339
## Run on a Device
283340

284341
Ionic's components work everywhere: on iOS, Android, and PWAs. To deploy to mobile, use [Capacitor](https://capacitorjs.com):

docs/vue/quickstart.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ Your new app's `src` directory will look like this:
6464
   └── HomePage.vue
6565
```
6666

67+
:::info
68+
All file paths in the examples below are relative to the `src/` directory.
69+
:::
70+
6771
Let's walk through these files to understand the app's structure.
6872

6973
## View the App Component
@@ -117,7 +121,7 @@ When you visit the root URL (`/`), the `HomePage` component will be loaded.
117121

118122
## View the Home Page
119123

120-
The Home page component, defined in `Views/HomePage.vue`, imports the Ionic components and defines the page template:
124+
The Home page component, defined in `views/HomePage.vue`, imports the Ionic components and defines the page template:
121125

122126
```html
123127
<template>
@@ -283,6 +287,61 @@ Note that we are passing the imported SVG reference, **not** the icon name as a
283287

284288
For more information, see the [Icon documentation](/docs/api/icon) and the [Ionicons documentation](https://ionic.io/ionicons/).
285289

290+
## Call Component Methods
291+
292+
Let's add a button that can scroll the content area to the bottom.
293+
294+
Update `views/NewPage.vue` to include a ref on `ion-content` and a button and some items after the existing icons:
295+
296+
```html
297+
<ion-content ref="content">
298+
<ion-button @click="scrollToBottom">Scroll to Bottom</ion-button>
299+
300+
<!-- Add lots of content to make scrolling possible -->
301+
<ion-item v-for="i in 50" :key="i">
302+
<ion-label>Item {{ i }}</ion-label>
303+
</ion-item>
304+
</ion-content>
305+
```
306+
307+
In the script section, add the new component imports and define the `scrollToBottom` function:
308+
309+
```html
310+
<script setup lang="ts">
311+
import {
312+
IonBackButton,
313+
IonButtons,
314+
IonButton,
315+
IonContent,
316+
IonHeader,
317+
IonIcon,
318+
IonItem,
319+
IonLabel,
320+
IonPage,
321+
IonTitle,
322+
IonToolbar,
323+
} from '@ionic/vue';
324+
import { heart, logoIonic } from 'ionicons/icons';
325+
import { ref } from 'vue';
326+
327+
const content = ref();
328+
329+
const scrollToBottom = () => {
330+
content.value.$el.scrollToBottom(300);
331+
};
332+
</script>
333+
```
334+
335+
To call methods on Ionic components:
336+
337+
1. Create a `ref` for the component
338+
2. Access the underlying Web Component via `$el`
339+
3. Call the method on the Web Component
340+
341+
This pattern is necessary because Ionic components are built as Web Components. The `$el` property gives you access to the actual Web Component instance where the methods are defined.
342+
343+
You can find available methods for each component in the [Methods](/docs/api/content#methods) section of their API documentation.
344+
286345
## Run on a Device
287346

288347
Ionic's components work everywhere: on iOS, Android, and PWAs. To deploy to mobile, use [Capacitor](https://capacitorjs.com):
@@ -312,17 +371,17 @@ Ionic Vue projects are created with TypeScript by default, but you can easily co
312371
npm uninstall --save typescript @types/jest @typescript-eslint/eslint-plugin @typescript-eslint/parser @vue/cli-plugin-typescript @vue/eslint-config-typescript vue-tsc
313372
```
314373

315-
2. Change the extension of all `.ts` files to `.js`. In a blank Ionic Vue app, this will be the `src/router/index.ts`, `src/main.ts`, and files in the `tests` directory.
374+
2. Change the extension of all `.ts` files to `.js`. In a blank Ionic Vue app, this will be the `router/index.ts`, `main.ts`, and files in the `tests` directory.
316375

317376
3. In `index.html`, change the imported `<script>` file from `/src/main.ts` to `/src/main.js`.
318377

319378
4. Remove `@vue/typescript/recommended` and `@typescript-eslint/no-explicit-any: 'off'` from `.eslintrc.js`.
320379

321-
5. Remove `Array<RouteRecordRaw>` and the import of `RouteRecordRaw` from `src/router/index.js`.
380+
5. Remove `Array<RouteRecordRaw>` and the import of `RouteRecordRaw` from `router/index.js`.
322381

323-
6. Remove `lang="ts"` from the `script` tags in any of your Vue components that have them. In a blank Ionic Vue app, this should only be `src/App.vue` and `src/views/HomePage.vue`.
382+
6. Remove `lang="ts"` from the `script` tags in any of your Vue components that have them. In a blank Ionic Vue app, this should only be `App.vue` and `views/HomePage.vue`.
324383

325-
7. Delete `tsconfig.json` and `src/vite-env.d.ts`.
384+
7. Delete `tsconfig.json` and `vite-env.d.ts`.
326385

327386
8. In package.json, change the build script from `"build": "vue-tsc && vite build"` to `"build": "vite build"`.
328387

0 commit comments

Comments
 (0)