I hope someone can help me
I want to send data from one page and then use it in another page from a service.
This is the parent component.ts
import { Component } from '@angular/core';
import { ShareService } from '../share.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
message:string = 'I am from home.ts';
constructor( private shareSvc: ShareService ) {}
ngOnInit() {
this.shareSvc.sharedMessage.subscribe(message => this.message = message)
}
}
This is my service
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ShareService {
private message = new BehaviorSubject<any> (null) ;
sharedMessage = this.message.asObservable();
constructor() { }
nextMessage(message: string) {
this.message.next(message)
}
}
And Finally, this is my last component where i want to get the data from home / service
import { Component, OnInit } from '@angular/core';
import { ShareService } from '../share.service';
@Component({
selector: 'app-pagina2',
templateUrl: './pagina2.page.html',
styleUrls: ['./pagina2.page.scss'],
})
export class Pagina2Page implements OnInit {
message:string;
constructor( private shareSvc: ShareService) { }
ngOnInit() {
this.shareSvc.sharedMessage.subscribe(message => this.message = message)
console.log(this.message);
}
}
If is neccesary, i'll post my page2 html:
<ion-header>
<ion-toolbar>
<ion-title>pagina2</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<h1>Message from Service and Home : {{message}}</h1>
</ion-content>
this is the result: enter image description here