1

I have two functions in two differents components, I'd like to share functions between them but I don't understand how to do and applicate it in my project. In a component, I have a getDatasFromMainTable(). And on the other one, I have a getDatasFromArchiveTable(). I tried :

@Input() myFirstComponent!: MyfirstComponent;

and in a function:

this.myFirstComponent.getDatasFromArchiveTable();

But I have a message

Cannot read properties of undefined (reading 'getDatasFromArchiveTable')

I read a lot of things about behaviorSubject, or @Input(), @ViewChild() or onChanges... But I don't know how to applicate it to my project if I need it for this.

4
  • Are the two components related at all? Is one a child of the other? Commented Apr 27, 2022 at 13:22
  • No, but I think there can be two children and maybe I need one parent component. Like a service. What do you think ? Commented Apr 27, 2022 at 13:23
  • You can have them both extend a superclass that is a Directive, and keep the shared state and behavior there. Commented Apr 27, 2022 at 13:27
  • Here is a tutorial to show how to do that: youtube.com/watch?v=gqZNiON_9-w Commented Oct 6, 2022 at 11:32

3 Answers 3

4

That's the purpose of a service.

Components should be "dumb" in the sense that they don't anything business-wise : they give responsibility to the services. That's what dependency injection is about.

So to share those methods, create a service that contains them, and let your components call those methods on your service.

Sign up to request clarification or add additional context in comments.

3 Comments

Any parent-child relation between them ?
@Octaz if you want to or not, up to you. A service is a singleton, it's not really a parent/child but rather a defined entity that you can use everywhere.
Thanks for your answer, I will build a service and see if it can fix my issue.
2

You have a lot of way to share data betwen component.

  1. Sharing Data via Input
  2. Sharing Data via ViewChild
  3. Sharing Data via Output() and EventEmitter
  4. Sharing Data with a Service
  5. Share same logic (method) betwen component

In your case the solution would be number 5. If you want to have sharable method (some logic and use it aproach the whole app)

Example of simple service:

import { Injectable } from '@angular/core';

@Injectable()
export class ExampleOfService{


  constructor() { }

  exampleOfFunction() {
   doSomething();
  }

}

And after that you can call your method in every component. You can add pameters etc...

  import { Component, OnInit  } from '@angular/core';
    
  @Component({
   selector: 'app-example-component',
   template:  ['./app-example-component.html'],
   styleUrls: ['./example.component.scss']
    })

export class ExampleComponent implements OnInit{
   constructor(private exampleOfService: ExampleOfService) { }

ngOnInit(){
     //you can subscribe or whatever you need.
     this.exampleOfService.exampleOfFucntion();
     }

  }

Comments

0

To manage a component in Angular that shares a few methods, you have 3 options:

Service: Create a shared service that contains the common methods and inject it into the components that need access to those methods. The shared service acts as a central place to hold the shared logic, and the components can use the service's methods as needed. This approach promotes code reuse and allows for easy maintenance and modification of the shared methods.

Inheritance: If you have multiple components that share common functionality, you can create a base component that includes the shared methods. Other components can then extend the base component to inherit the shared methods. This approach is useful when the components have similar behavior but require slight variations or additional functionality.

Abstract Class: Similar to inheritance, you can create an abstract class that includes the shared methods. Components can then extend the abstract class to inherit the methods. The abstract class serves as a blueprint for the components, providing a common set of methods that can be reused across multiple components.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.