0

how do I pass some data between pages in Ionic 4? Consider this example: In file1.ts I have:

export class ArchivePage{

 constructor() { }
 messages = [
    {
      user: 'user1',
      msg: 'Hello!',
      type: 0
    },
    {
      user: 'user2',
      msg: 'Hi!',
      type: 1
    }
   ];
}

Now in file1.html I can simply use *ngFor = "let message of messages" and {{message.user}} {{message.msg}}. But I have another file2.ts, how can I pass this messages to file2.ts in order to use them in file2.html?

Thanks a lot

this.jind
  • 319
  • 1
  • 4
  • 14
  • 1
    I suppose you are using ionic with angular, if this is your case then another question, what does your files contain, is it a component?, a service? a directive? a pipe? use these terms instead of file, because the files can contain any thing, and the way to passe information differ from one another – gaetan224 Jul 17 '20 at 12:27
  • Yes sorry, I'm using Ionic with Angular, and my files are components. I wanna pass information between components. – this.jind Jul 17 '20 at 12:31
  • 1
    are they related? does one component use another one? if that is your case you can use Angular input decorator https://angular.io/api/core/Input – gaetan224 Jul 17 '20 at 12:38
  • No they are not. I think I have to use NavController and NavParams right? – this.jind Jul 17 '20 at 12:59
  • 1
    if these are public data yes you can use it – gaetan224 Jul 17 '20 at 13:23

1 Answers1

1

You have two options, you can use a shared service for your data as data source and get data from it in the ts files, or you can use Storage for setting your messages in one ts file and retrieve it in the second file, for more details for the second solution, please check the attached documentation link https://ionicframework.com/docs/angular/storage

Emad Abdou
  • 277
  • 1
  • 7
  • You also have another choice, you can pass data in your route, see the attached comment link for more information ` https://stackoverflow.com/a/36835156/5710264 ` – Emad Abdou Jul 18 '20 at 15:50