To implement this functionality, Global Service/shared Service must be utilized. Initially, set your global variable with a value globally in your application so that you can access the service from anywhere within your app.
Let's assume this is our Global service:
export class GlobalService {
array: string[];
}
export class HomePage {
array: any[] = [];
constructor(private global: GlobalService){
this.array = this.global.array;
}
}
export class SecondPage {
array: any[] = [];
constructor(private global: GlobalService){
this.array = this.global.array;
}
}
You can inject the GloablService globally in your app, such as in the main module, eliminating the need to add the service to the list of providers every time. Simply import and initialize it in the constructor to access its methods and variables.