0

I develop an Angular application using Typescript and I wonder how to refresh a tab from another.

I have two pages A and B: I have a button in A to reach B and when I clicked this button I want to reload B

page A

import {Component, OnInit} from '@angular/core';
import {UserDto} from '../api/models/user-dto';
import {UserService} from '../services/user.service';

@Component({
  selector: 'app-tabs',
  templateUrl: 'tabs.page.html',
  styleUrls: ['tabs.page.scss']
})
export class TabsPage  implements OnInit{
  user: UserDto;
  win: any;
  newTab: any;

  constructor(
      private userService: UserService,
  ) {}

  display(){
    return localStorage.getItem('userMail') != null;
  }

  ngOnInit(): void {
    if (localStorage.getItem('userMail') !== null) {
      this.getUser();
    }
  }
  getUser() {
    this.userService.getUserByEmail(localStorage.getItem('userMail'))
        .subscribe(user => this.user = user,
            error => console.log('error'),
            () => console.log('complete'));
  }

  refreshMatch() {
    //i want to refresh match page (page B) here
  }
}

//html part

<ion-tabs>
  <ion-tab-bar slot="bottom">
    <ion-tab-button tab="tab2"  *ngIf="display()" (click)="refreshMatch()">
      <ion-icon name="chatbox-ellipses-outline"></ion-icon>
      <ion-label>Matchs</ion-label>
    </ion-tab-button>

    <ion-tab-button tab="tab1">
      <ion-icon name="home-outline"></ion-icon>
      <ion-label>Accueil</ion-label>
    </ion-tab-button>

    <ion-tab-button tab="me"  *ngIf="display()">
      <ion-icon name="person-circle-outline"></ion-icon>
      <ion-label>Moi</ion-label>
    </ion-tab-button>
  </ion-tab-bar>

</ion-tabs>

page B

import {Component, ElementRef, OnInit, Renderer2, ViewChild, } from '@angular/core';
import {UserDto} from '../api/models/user-dto';
import {UserService} from '../services/user.service';


@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss']
})
export class Tab2Page implements OnInit{

  matchs = [];

  // replace with user
  match = 'utilisateur';

  user: UserDto;

  constructor(private userService: UserService, ) {}

  async ngOnInit() {
    if (localStorage.getItem('userMail') !== null) {
      this.getUser();
      this.matchs = await this.getMatchs();
      console.log(this.matchs);
    }
  }

  getMatchs() {
    return this.userService.getUserMatch(localStorage.getItem('userMail')).toPromise();
  }

  getUser() {
    this.userService.getUserByEmail(localStorage.getItem('userMail'))
        .subscribe(user => this.user = user,
            error => console.log('error'),
            () => console.log('complete'));
  }

  refresh(): void {
    window.location.reload();
  }
}

//html

<ion-header [translucent]="true">
  <ion-toolbar><ion-title>Matchs</ion-title></ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true">

  <ion-button shape="round" size="small" (click)="refresh()"><ion-icon name="reload-outline"></ion-icon>refresh</ion-button>

  <ion-card *ngFor="let user of matchs">
    <ion-card-title>{{user.name}} {{user.family | uppercase}}</ion-card-title>
    <ion-card-content>
      <ion-label>{{user.country}}</ion-label>
    </ion-card-content>
  </ion-card>

</ion-content>

I have a method to generate components which I call in ngOnInit method but I want to call the method which generate component in a OnShow style method

How can I do that in Typescript, please

Heretic Monkey
  • 11,078
  • 7
  • 55
  • 112
Gauthier
  • 51
  • 4
  • There's a similar question [here](https://stackoverflow.com/questions/28230845/communication-between-tabs-or-windows). – D M Jan 13 '21 at 21:17
  • this is not what i mean, but thx – Gauthier Jan 13 '21 at 21:35
  • 1
    Use a service to pass events between pages. Can you show me what your code looks like so that I can help? – emmanuel agarry Jan 13 '21 at 21:43
  • Does this answer your question? [How can I refresh a tab from another using Javascript](https://stackoverflow.com/questions/10662107/how-can-i-refresh-a-tab-from-another-using-javascript) – Heretic Monkey Jan 13 '21 at 21:53
  • TypeScript is a superset of JavaScript all valid strict JavaScript is valid TypeScript, so be sure to search for JavaScript solutions when looking for answers. – Heretic Monkey Jan 13 '21 at 21:54
  • Thanks Monkey but when i use this, it just opened a new tab and this is not what I want – Gauthier Jan 13 '21 at 21:54
  • I set up a project on stackblitz here. You can check it out. https://stackblitz.com/edit/ionic-nvwrw4?file=pages%2Fabout%2Fabout.html When you click the buttons on the home page, The messages can be seen on the about page. That is one way to communicate in an Angular app. I hope this helps – emmanuel agarry Jan 13 '21 at 23:02
  • https://angular.io/guide/lifecycle-hooks#responding-to-view-changes Can't you just use the existing lifecycle hooks? ngDoCheck or ngAfterViewChecked – James D Jan 14 '21 at 09:52

0 Answers0