Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the app.component variables and methods from other pages in Ionic?

I want to access and change app.component.ts variable or access methods from other pages (otherpage.ts) or other components such as;

app.component.ts

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  accessedVariable: any;

  constructor(){ }

  accessedMethod() {
   ..something
  }

}

otherpage.ts

@Component({
  selector: 'page-other',
  templateUrl: './otherpage.html',
})
export class OtherPage {

  constructor() { }
}
like image 654
Dr. X Avatar asked Jan 30 '23 12:01

Dr. X


1 Answers

18-02-2020

Please don't use Event emitter. Use the Observable pattern. Otherwise, you'll have to face issues when updating to the Ionic 5. i.e. no Event API on Ionic 5.

Original

You can do this in a number of ways.

One method where I can tell you is using Events.

Events is a publish-subscribe style event system for sending and responding to application-level events across your app.

Another method may be using the provider.On that use case, you can share your methods and variables through the provider.

like image 91
Sampath Avatar answered Feb 05 '23 14:02

Sampath