How can I force the reload of parent component from its child component?

This app was written using Angular 17 (standalone) and Ionic 7. Tab1Page (“cards”) is the parent and ItemCardComponent is its child (a Swiper carousel of ion-cards which values are recovered from a Firestore Database):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class Tab1Page {
slides: itemData[] = [];
constructor(public itemService: ItemService) {
this.ionViewDidEnter();
}
ionViewDidEnter() {
this.itemService.getThings().subscribe((items: itemData[]) => {
const extra: itemData = {
user: '',
name: '',
picture: ''
}
items.unshift(extra)
this.slides = items;
});
}
}
</code>
<code>export class Tab1Page { slides: itemData[] = []; constructor(public itemService: ItemService) { this.ionViewDidEnter(); } ionViewDidEnter() { this.itemService.getThings().subscribe((items: itemData[]) => { const extra: itemData = { user: '', name: '', picture: '' } items.unshift(extra) this.slides = items; }); } } </code>
export class Tab1Page {
 slides: itemData[] = [];

 constructor(public itemService: ItemService) {
    this.ionViewDidEnter();
 }

 ionViewDidEnter() {
  this.itemService.getThings().subscribe((items: itemData[]) => {
   const extra: itemData = {
        user: '',
        name: '',
        picture: ''
   }
   items.unshift(extra)
   this.slides = items;
  });
 }
}

From ItemService:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>getThings(): Observable<itemData[]> {
const itemsRef = collection(this.firestore, 'userItems');
const q = query(itemsRef, where('user', '==', this.auth.currentUser!.uid) );
return collectionData(q, { idField: 'id'}) as Observable<itemData[]>;
}
</code>
<code>getThings(): Observable<itemData[]> { const itemsRef = collection(this.firestore, 'userItems'); const q = query(itemsRef, where('user', '==', this.auth.currentUser!.uid) ); return collectionData(q, { idField: 'id'}) as Observable<itemData[]>; } </code>
getThings(): Observable<itemData[]> {
    const itemsRef = collection(this.firestore, 'userItems');  
    const q = query(itemsRef, where('user', '==', this.auth.currentUser!.uid) );
    return collectionData(q, { idField: 'id'}) as Observable<itemData[]>;
  }

NOTE: First card (‘extra’) is always blank and it’s used for data input.
first card of the Swiper carousel

When a new card is saved (“setThing” is called from the child “ItemCardComponent”), data is stored as a new item to ‘userItems’ (Firestore collection).
From ItemService:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>setThing(thingUnit: itemData) {
thingUnit.user = this.auth.currentUser?.uid;
const itemsRef = collection(this.firestore, 'userItems');
return addDoc(itemsRef, thingUnit);
}
</code>
<code>setThing(thingUnit: itemData) { thingUnit.user = this.auth.currentUser?.uid; const itemsRef = collection(this.firestore, 'userItems'); return addDoc(itemsRef, thingUnit); } </code>
setThing(thingUnit: itemData) {
  thingUnit.user = this.auth.currentUser?.uid;
  const itemsRef = collection(this.firestore, 'userItems');
  return addDoc(itemsRef, thingUnit); 
}

I think I get to a correct diagnose of the issue, but I’m stuck on what to do to solve it.

Everything works fine when it come to save (and recover) data from Firestore. Except for the carousel view, which goal was to be updated in real time, but currently requires a manual refresh of Tab1Page to show the new slide. Obviously, It doesn’t update because the saving event (that occurs in child) doesn’t trigger any kind of change to the slide array (that relies on the load/reload of parent). The calls that updates this.slides array with the values from “items” will only happen when the constructor and ionViewDidEnter() run.

What commands should I use to force a refresh of Tab1Page (parent), taking into account that I’ll need to run it from the ItemCardComponent (child), together with the card saving/editing functions?

I guess if that should be other solutions to this, because I understand that this current code implies too much calls to Firebase and that’s not the optimal scenario.

Recognized by Google Cloud Collective

With the little code-snippet and your error description in the question, I came up with the following workaround/solution. I wrote it down as a kind of pseudocode

There are several ways to send data between components. Since you are dealing with parent and child components, the easiest way to do this is via @Output() (child -> parent) and an event emitter.

Add the @Output() property as class variable to the child -component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Output() saveNewItemEvent = new EventEmitter<void>();
</code>
<code>@Output() saveNewItemEvent = new EventEmitter<void>(); </code>
@Output() saveNewItemEvent = new EventEmitter<void>();

Now execute this event, by saving new items in child-component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>this.saveNewItemEvent.emit();
</code>
<code>this.saveNewItemEvent.emit(); </code>
this.saveNewItemEvent.emit();

In parent component, where you call child-component, you can now listen to this event:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><app-child-component>
(saveNewItemEvent)="reloadItems($event)"
<app-child-component>
</code>
<code><app-child-component> … (saveNewItemEvent)="reloadItems($event)" … <app-child-component> </code>
<app-child-component>
    …
    (saveNewItemEvent)="reloadItems($event)"
    …
<app-child-component>

Within reloadItems() in parent-component, you can reload/load datas now to achieve the new saved items, where you needed after new savings changes in child-component

3

This is how I got the app to have “acceptable” behavior for the moment:

When new items are saved, child component emits the event (as it was well suggested by @SwissCodeMen) to call a function in the parent component .

Decorator declaration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Output() reloadParentEmitter = new EventEmitter<boolean>();
</code>
<code>@Output() reloadParentEmitter = new EventEmitter<boolean>(); </code>
@Output() reloadParentEmitter = new EventEmitter<boolean>();

Triggering the event from the child’s “saveItem” function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>this.reloadParentEmitter.emit(true);
</code>
<code>this.reloadParentEmitter.emit(true); </code>
this.reloadParentEmitter.emit(true);

“Capturing” the event from the parent side (tab1.page.html):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><app-item-card (reloadParentEmitter)="reloadItems($event)"></app-item-card>
</code>
<code><app-item-card (reloadParentEmitter)="reloadItems($event)"></app-item-card> </code>
<app-item-card (reloadParentEmitter)="reloadItems($event)"></app-item-card>

And, finally, running the “routing approach” from the function that’s called in “tab1.page.ts” (parent):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>reloadItems(reloading: boolean){
this.router.navigateByUrl('blankCanvas', { skipLocationChange: true }).then(() => {
this.router.navigate(['reload']);
});}
</code>
<code>reloadItems(reloading: boolean){ this.router.navigateByUrl('blankCanvas', { skipLocationChange: true }).then(() => { this.router.navigate(['reload']); });} </code>
reloadItems(reloading: boolean){
this.router.navigateByUrl('blankCanvas', { skipLocationChange: true }).then(() => {
  this.router.navigate(['reload']);
});}

As I just wanted a reload of the ‘ion-tab’ (parent) where the ‘app-item-card’ (child) is nested, all that I needed was a quick navigation to any other path and back again to the original one. So I generated a blank page with the same background of the entire app and pointed my first jump to it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>this.router.navigateByUrl('blankCanvas', { skipLocationChange: true })
</code>
<code>this.router.navigateByUrl('blankCanvas', { skipLocationChange: true }) </code>
this.router.navigateByUrl('blankCanvas', { skipLocationChange: true })

And than, back to the original path, where data is already updated:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>this.router.navigate(['reload'])
</code>
<code>this.router.navigate(['reload']) </code>
this.router.navigate(['reload'])

To achieve the correct routes I edited path values in “app.routes.ts”:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{path: 'reload', loadChildren: () => import('./pages/private/tabs/tabs.routes').then((m) => m.routes)},
{path: 'blankCanvas', loadComponent: () => import('./pages/private/shared/components/blankCanvas/blankCanvas.page').then( m => m.BlankCanvasPage)}
</code>
<code>{path: 'reload', loadChildren: () => import('./pages/private/tabs/tabs.routes').then((m) => m.routes)}, {path: 'blankCanvas', loadComponent: () => import('./pages/private/shared/components/blankCanvas/blankCanvas.page').then( m => m.BlankCanvasPage)} </code>
{path: 'reload', loadChildren: () => import('./pages/private/tabs/tabs.routes').then((m) => m.routes)},
{path: 'blankCanvas', loadComponent: () => import('./pages/private/shared/components/blankCanvas/blankCanvas.page').then( m => m.BlankCanvasPage)}

And, in “tabs.routes.ts”, I added a copy of the original tab path, renamed to ‘reload’:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{path: 'reload', redirectTo: '/tabs/tab1', pathMatch: 'full'}
</code>
<code>{path: 'reload', redirectTo: '/tabs/tab1', pathMatch: 'full'} </code>
{path: 'reload', redirectTo: '/tabs/tab1', pathMatch: 'full'}

As I said, that’s none but the “acceptable” solution I got to, right now. But I will keep investigating and I’m sure I’ll find a better approach to it.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật