To implement shared parameters, begin by creating a service as shown below:
import { Injectable, EventEmitter } from '@angular/core';
import { Subscription } from 'rxjs/internal/Subscription';
@Injectable({
providedIn: 'root'
})
export class SharedParamsService {
invokeFirstComponentFunction = new EventEmitter();
subsVar: Subscription;
constructor() { }
onUpdateEnv(key, val) {
var toEmit = [key, val]
this.invokeFirstComponentFunction.emit(toEmit);
}
}
Once the service is set up, you can trigger an event when a button is clicked in your component:
recalcButton(){
this.sharedParams.onUpdateEnv("btn", this.jobObj.name);
}
Next, in the component where you want to update based on the button click, subscribe to the emitter in the shared service:
this.sharedParams.subsVar = this.sharedParams.
invokeFirstComponentFunction.subscribe((emitted) => {
if (emitted[0] == "btn") {
this.ngOnInit();
}
});
While using key-value pairs is optional, it allows for flexibility in handling multiple events or variables.