Transferring functionality from a child component to a parent component

I have a Base Component called ListComponent and a ParentComponent named Businesses2ListComponent. The concept is to have multiple types of Lists with tables that all stem from the ListComponent. This requires passing the correct service accordingly.

In this scenario, I need to pass the BusinessService (which is an extension of BaseService) to the BaseComponent.

Here's the structure of the ListComponent:

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent<S extends BaseService<T, L>, T, L> implements OnInit {

  constructor(private viewContainerRef: ViewContainerRef,
              private domElement: ElementRef,
              private ngForage: NgForage,
              private route: ActivatedRoute,
              private router: Router,
              private injector: Injector,
              private dataService: S) {
  }
  
}

This is the definition of my Business2ListComponent:

@Component({
  selector: 'app-businesses2-list',
  templateUrl: './businesses2-list.component.html',
  styleUrls: ['./businesses2-list.component.scss']
})
export class Businesses2ListComponent extends ListComponent<BusinessService, Business, BusinessList> implements OnInit {

  constructor(viewContainerRef: ViewContainerRef,
              domElement: ElementRef,
              ngForage: NgForage,
              dataService: BusinessService,
              route: ActivatedRoute,
              router: Router,
              injector: Injector) {
    super(viewContainerRef, domElement, ngForage, route, router, injector, dataService);
  }

  ngOnInit() {
  }

}

Upon trying to execute this setup, I encountered errors: https://i.sstatic.net/n9dVO.png https://i.sstatic.net/dJ9e7.png

I suspect the issue lies in the final argument, but what other approach should I consider?

Answer №1

Seeing a question mark in Angular usually indicates that the object being injected cannot be found. There are several potential causes for this issue, but one common starting point is to check your module file. Make sure you have properly included and instantiated BusinessService in the providers array of your module.

Answer №2

You are unable to perform that action within your constructor when: private dataService: S.

You have two choices: You can adjust your constructor to accept it in this way:

private dataService: new () => S

There may be a TSLint error in your BusinessListComponent constructor when you invoke super(), but it will function as intended.

Alternatively, my preferred approach in your scenario involves using OOP, as I do not see a need to pass in BusinessService in your

ListComponent<BusinessService, Business, BusinessList>
. Here is a stackblitz demonstration of this technique.

In your BusinessListComponent

export class BusinessListComponent extends ListComponent<Business, BusinessList> implements OnInit {

  constructor(//...your args, private businessService: BusinessService) { 
    super(// ... your args, businessService);
  }

  ngOnInit() {
  }
}

Your ListComponent would look like this:

export class ListComponent<T, L> implements OnInit {
  constructor(private dataService: BaseService<T, L>) { 
  }
  ngOnInit() {
  }
}

And for your BusinessService

export class BusinessService extends BaseService<Business, BusinessList> {
  constructor() {
    super();
  }
}

I hope that clears things up for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The JWT Token is not being sent to the allowedDomain or whitelistedDomains in auth0/angular-jwt due to a configuration issue involving Regexp

Currently, I am utilizing the auth0/angular-jwt library to inject JWT tokens into my application. The documentation for this library suggests that configuration can be achieved by using strings or RegExp for specifying allowed domains. However, upon implem ...

How can I create an Array of objects that implement an interface? Here's an example: myData: Array<myInterface> = []; However, I encountered an issue where the error message "Property 'xxxxxx' does not exist on type 'myInterface[]'" appears

Currently, I am in the process of defining an interface for an array of objects. My goal is to set the initial value within the component as an empty array. Within a file, I have created the following interface: export interface myInterface{ "pictur ...

Leveraging the power of Angular 5 to seamlessly integrate two distinct components on

I am exploring a way to render an additional component on the current page instead of navigating to a new one. Here is my setup: <button mat-button color="primary" [routerLink]="['/tripspath', trip.tripId]" style="cursor: pointer">View Rou ...

Can you please explain the significance of classes <T> and <U> in Angular 2?

After diving into the Angular 2 TypeScript documentation, I have come across these two classes frequently. Can someone provide a detailed explanation of what they are? One example code snippet from the QueryList API documentation showcases: class QueryLi ...

Adjustable Material UI Switch that reflects the value, yet remains changeable

I am facing a challenge with integrating a Material UI switch (using Typescript) to showcase a status value (active/inactive) on my edit page, and making it toggleable to change the status. I have been able to either display the value through the switch OR ...

Formula for a distinct conclusion of written words

Can I restrict input to only letters and numbers, with words like xls, xlsx, json, xml always appearing at the end of the line? This is the pattern I am currently using: Validators.pattern(/^[a-zA-Z0-9]+$/) ...

Using Flickity API in Vue 3 with Typescript Integration

I have encountered an issue with implementing Flickity in my Vue 3 application. Everything works perfectly fine when using a static HTML carousel with fixed cells. However, I am facing difficulties when attempting to dynamically add cells during runtime us ...

How can I effectively retrieve a value from Ionic storage and use it as an authorization token instead of receiving the promise "Bearer [object Promise]"?

In my Ionic 4 angular project, the storage.get('token').then() function returns a promise object instead of the refresh token. For JWT authentication, I send the access token as the bearer token in the authorization headers using an HTTP interce ...

Adding bootsprap css-4 to an Angular Java project using Maven

I have developed an Angular application with a Java-Spring backend framework. I have successfully built both the frontend and backend using Maven. To build the Angular application, I utilized the frontend-maven-plugin and installed Bootstrap 4 by running: ...

Aligning Material UI Input with Select Component

I have encountered an issue regarding the alignment of my elements on Material UI. Provided below is the code for my Input and Select elements: <div> <form> <TextField label="Search" /> <FormContro ...

Left-to-right animation of Angular Material progress bar buffer

Hey there, I'm interested in creating a progress bar with buffer animation, but the current animation goes from right to left. I'd like it to go from left to right, or have no animation at all. I've searched through the progress bar API, but ...

Multiple asynchronous calls in Angular 2

In my Component, there is a function that is supposed to return a value (Promise). This value requires information from two distinct sources: an API call and data from a database. The method in question looks like this: public getValue(): Promise<numb ...

The function that iterates through the 'categoria' state and returns a new array is not functioning properly

Having difficulty with the object of a function using .map(). It works when the code is used directly, but not when put inside a function. For example: if(this.state.cat){ return _.map(this.state.cat, categoria => { if(this.state.search_ ...

Angular 6: Dealing with Type Errors in the HttpClient Request

I am encountering issues with my services. I am attempting to retrieve a list of media files (generated by NodeJS, which generates a JSON file containing all media items). Although I can successfully fetch the data, I am facing an error stating "Property & ...

Leveraging async-await for carrying out a task following the deletion of a collection from a mongodb database

Trying to implement async await for executing an http request prior to running other code. Specifically, wanting to delete a collection in the mongodb database before proceeding with additional tasks. This is what has been attempted: app.component.ts: ...

Is it possible to assign a property value to an object based on the type of another property?

In this illustrative example: enum Methods { X = 'X', Y = 'Y' } type MethodProperties = { [Methods.X]: { x: string } [Methods.Y]: { y: string } } type Approach = { [method in keyof Method ...

What steps can I take to change my single-line navbar into a two-line design?

Lately, I've been working on a website with a navbar that looks like the one in this https://i.sstatic.net/5ptAj.png image. However, I want to change my navbar to look more like the one in this https://i.sstatic.net/KiHCf.png image. I've attemp ...

Listening to changes in a URL using JQuery

Is there a way to detect when the browser URL has been modified? I am facing the following situation: On my webpage, I have an iframe that changes its content and updates the browser's URL through JavaScript when a user interacts with it. However, no ...

What is the functionality of angular's @Attribute decorator?

Just starting out with Angular and diving into the world of decorators. While exploring the angular.io documentation, I found limited information on the @Attribute decorator. Can anyone provide me with some practical use cases for this? ...

Sticky header in React data grid

Is there a way to implement a sticky header for a data grid in react? I have tried various methods but haven't been able to figure it out. Any suggestions would be appreciated. You can find my code sandbox example here. #react code export const Styl ...