title: How to share function between sibling components in angular 4
body: <p>I have been reading that I use a sharedservice to share data between 2 components (siblings) but what about sharing functions? </p>
  
  <p>For example I have the following </p>
  
  <p>Service</p>
  
  <pre><code>@Injectable()
  export class SharedFunctionService {
  
    //set these functions from the component in which it resides
    //name scheme: componentname + function name + fn 
    comp1CancelFn: Function;
    constructor() { }
  }
  </code></pre>
  
  <p>comp1</p>
  
  <pre><code>export class comp1 {
  
      constructor(private sharedFunctionService: SharedFunctionService, 
                  private router:Router, private route: ActivatedRoute) {
          this.sharedFunctionService.comp1CancelFn= this.onCancel;
      }
       onCancel():void{
           this.router.navigate(['../'], {relativeTo:this.route});
  
      }
  }
  </code></pre>
  
  <p>Then I have comp2 </p>
  
  <pre><code> export class comp2{
     constructor(private sharedFunctionService: SharedFunctionService, 
                 activatedRoute: ActivatedRoute, private router: Router) { 
      }
     onCancel(){
         this.sharedFunctionService.comp1CancelFn();
     }
   }
  </code></pre>
  
  <p>So I save the function from comp1 to the service. My expected result is if the cancel function is called from some piece of the html of comp2, it will look for the function in the service and call it. It does this but the problem I run into is all the variables that reside in comp1 are considered undefined (in this case the router but even if i set a simple name string and try to console log it, it is also undefined). Is there any way around this. I came up with my current solution from this <a href="https://stackoverflow.com/questions/42405038/call-a-function-in-a-component-from-another-component/42405146#42405146">source</a> and this <a href="https://stackoverflow.com/questions/41958836/angular2-interaction-between-components-using-a-service/41960007#41960007">source</a>. </p>
  
  <p>Currently using angular 4.4.4 release and the service is in my root providers in app.module</p>