How to invoke method from one component to another component in Angular 2?

Viewed 49617

I have two different component - one component has click event method for the list users & I want to inherit(invoke) that click event method on detail page(another component) )

How to do that, Please help me...

ANSWER UPDATE

Thanks For your Answers.

I have got the solution by Using extend Class & using super() in constructor.

First Class

export class TeamsView {
   constructor(private test:TestService){
   }
   ...
}

Second Class

export class TeamsForm extends TeamsView {
   constructor(private test:TestService, private router: Router){
     super(test)
   }
   ...
}
6 Answers

I have got the solution by Using extend Class & using super() in the constructor.

First Class

export class TeamsView {
    constructor(private test:TestService){
    }
    ...
 }

Second Class

export class TeamsForm extends TeamsView {
    constructor(private test:TestService, private router: Router){
      super(test)
    }
    ...
}

I don't know its real method for call the class in the constructor for resolving this but I have found this from one of the Angular sites, and its working for me as well.

1.) First, you need to import the first component into your target component.

import { FirstComponent } from '../first.component';

2.) Then you need to create an object of the first component like this:

FirstComponentObject = new FirstComponent();

3.) Then call a function of the first component from the target(current) component

FirstComponentObject.FirstComponentFunction();

If you need any help, please comment. Thanks for reading.

Related