-1

Trying to pass values by method(function) from one component to another component using extends. But not working. Is it possible by any service?. So How to resolve this issue? If anyone knows please help to find the solutions.

bus.component.ts:

export class BusComponent extends CarComponent implements OnInit {
  constructor() {
    super();
    this.getValues('2', '5');
  }
  ngOnInit() {}
}

car.component.ts:

export class CarComponent implements OnInit {
  constructor() {}
  ngOnInit() {}
  getValues(a: any, b: any) {
    console.log(a + '===' + b);
  }
}

Demo : https://stackblitz.com/edit/angular-ivy-mb9wjg?file=src%2Fapp%2Fcar%2Fcar.component.ts

N.F.
  • 3,844
  • 3
  • 22
  • 53
EMahan K
  • 417
  • 1
  • 19

1 Answers1

0

In your code you have to use the BusComponent somewhere. If you don't use this component, the constructor will not be called. e.g. you can add <app-bus></app-bus> in app.component.html and you will see the text in console.

https://stackblitz.com/edit/angular-ivy-tqg741?file=src/app/app.component.html

MG7
  • 119
  • 13
  • supper() from BusComponent will call the constructor of CarComponent and because the CarComponent constructor has a parameter you have to call super with httpClient as parameter – MG7 Jul 21 '22 at 18:36
  • Can you update in the stackblitz? – EMahan K Jul 21 '22 at 18:41
  • Also have to import HttpClientModule in app.module.ts https://stackblitz.com/edit/angular-ivy-92sird?file=src%2Fapp%2Fshared%2Fbus%2Fbus.component.ts,src%2Fapp%2Fcar%2Fcar.component.ts,src%2Fapp%2Fapp.module.ts – MG7 Jul 21 '22 at 18:42
  • Any other simple way is there to pass the values? – EMahan K Jul 21 '22 at 19:33
  • What do you want to do with these values? Is not clear what you want to achieve. You can declare them as properties in CarComponent and initialize them in BusComponent, but I don't know for what you need these values, depending on what you need them there could be a different way to pass the values. Also you don't need HttpClient in order to pass the values, but I suppose you need this for something else. – MG7 Jul 21 '22 at 19:40
  • Getting error. If i use httpclient. Any ,Other method is there? I mean without extends class ? – EMahan K Jul 21 '22 at 19:43
  • I posted in the previous comments the stackblitz with code where httpclient is not used and is working and another stackblitz code with HttpClient that is working. – MG7 Jul 21 '22 at 19:49
  • Is it possible by service? – EMahan K Jul 21 '22 at 20:10
  • Better if it by service. you have any idea? – EMahan K Jul 21 '22 at 20:30
  • by creating and using a service is an option also. but why this way from here https://stackblitz.com/edit/angular-ivy-tqg741?file=src/app/app.component.html is not good for you? – MG7 Jul 21 '22 at 20:33
  • and there is also this way by declaring properties in CarComponent https://stackblitz.com/edit/angular-ivy-tqg741?file=src%2Fapp%2Fcar%2Fcar.component.ts,src%2Fapp%2Fshared%2Fbus%2Fbus.component.ts – MG7 Jul 21 '22 at 20:42