As mentioned in the comments, you need to use a service something like this:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class PreviousUrlService {
previousUrl: Observable<string>;
private previousUrlSubject: Subject<string>=new Subject();
constructor() {
this.previousUrl = this.previousUrlSubject.asObservable();
}
updatePreviousUrl(prevUrl: string) {
this.previousUrlSubject.next(prevUrl);
}
}
And the components need to be injected with the service.
export class ComponentAComponent implements OnInit {
previousUrl:string;
constructor(private previousUrlService: PreviousUrlService) {
}
ngOnInit() {
this.previousUrlService.previousUrl.subscribe((data) => {
this.previousUrl=data;
console.log('>> previous url', this.previousUrl);
});
}
}
And then component B can change the value and it fires in component A subscription function
export class CompBComponent implements OnInit {
constructor(private previousUrlService: PreviousUrlService) {
}
ngOnInit() {
}
onUpdateUrl(prevUrl: string){
this.previousUrlService.updatePreviousUrl('http://google.com');
}
}
This is the general idea experiment with different types of subjects to suit ur needs.