What is the best way to categorize variables?

How can I organize variables together:

export let findbyc$: Observable<Object>;
export let findbyi$: Observable<Object>;
export let findbyo$: Observable<Object>;
export let findbyob$: Observable<Object>;

I would like to group them as follows:

const ChildObservers {
    export let findbyc$: Observable<Object>;
    export let findbyi$: Observable<Object>;
    export let findbyo$: Observable<Object>;
    export let findbyob$: Observable<Object>;
}

Then, at a later point in the application, I intend to use it like this: ChildObservers.findbyc$

Answer №1

If you're looking to define a set of properties that are grouped but not initialized, consider creating an interface and exporting a variable of that type.

interface ChildObservers {
    findbyc$?: Observable<Object>;
    findbyi$?: Observable<Object>;
    findbyo$?: Observable<Object>;
    findbyob$?: Observable<Object>;
}

export const observers: ChildObservers = { };

You can then initialize these properties as needed.

observers.findbyob$ = this.httpClient.get();

Additionally, I recommend making the interface generic to match the generic argument of the Observable<T>.

interface ChildObservers<TResult> {
    findbyc$?: Observable<TResult>;
    findbyi$?: Observable<TResult>;
    findbyo$?: Observable<TResult>;
    findbyob$?: Observable<TResult>;
}

export const obsersers: ChildObservers<Object> = { };

Make sure to initialize it correctly

obsersers.findbyc$ = this.http.get<Object>();

Answer №2

export class ChildObservers {
  public constructor(private readonly httpService: HttpService){}

  public fetchDataByCategory$ = (): Observable<Object> => {
    return this.httpService.get();
  };

  public fetchDataById$ = (): Observable<Object> => {
    return this.httpService.get();
  };

  public fetchDataByOwner$ = (): Observable<Object> => {
    return this.httpService.get();
  };

  public fetchDataByObject$ = (): Observable<Object> => {
    return this.httpService.get();
  };
}

// Example for using the APIs
class MyComponent {
  public constructor(private readonly childObservers: ChildObservers){}

  public callApiFunction(){
    this.childObservers.fetchDataByCategory$.subscribe(response => {
      console.log(response);
    });
  }
}

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

Retrieve the value of the Type Property dynamically during execution

Looking at this type generated from a graphql schema: export type UserPageEntry = { readonly __typename?: 'UserPageEntry' } I am interested in retrieving the string representation of its only property type ('UserPageEntry') during co ...

Using an Object as a parameter in a Typescript function

I am currently working on an Angular component that includes a function. Within this function, I need to pass an Object as a parameter and invoke the function with these parameters. It has been some time since I last worked with Angular, where "any" was ty ...

Implementing a 12-month display using material-ui components

Just starting out with ReactJs, TypeScript, and material-ui. Looking to display something similar to this design: https://i.stack.imgur.com/zIgUH.png Wondering if it's achievable with material-ui. If not, any suggestions for alternatives? Appreciate ...

Error with object props in React using Typescript

Here's a scenario; I have a list of 'Reviews' that I am trying to render. The Proptype for these reviews is as follows: export interface Props { title: string; name: string; reviewdesc: string; rating: number; } In the pare ...

Event for changing Ionic 2 page

Is there a way to execute code every time the page changes without adding an ngOnDestroy method to every page in Ionic 2? Instead of using Ionic 2 page lifecycle hooks like ionViewDidUnload, is there a simpler solution by adding a single method to the mai ...

Guide to integrating a component within another component

Due to the size of this application, I am unable to utilize app.module. Unfortunately, the component I need to implement does not contain a module file. I have attempted to import it and utilize @NgModule, but it has not been successful. An error occur ...

Length of Angular events

I am embarking on the journey of building an Angular web application and I find myself in need of tracking various performance time-related data: The duration between clicking a button and the API call being initiated The duration between the API call be ...

Creating a personalized menu using Nextron (electron) - Step by step guide

I'm currently in the process of developing an app with Nextron (Electron with Nextjs and Typescript). Although I have the foundation of my Next app set up, I've been encountering issues when attempting to create a custom electron menu bar. Every ...

An effective method for targeting a specific button within a CSS file

I have multiple button tags in my code, but I need to style a specific one using CSS. How can I target this particular button and apply styles only to it while leaving the others unchanged? Do I need to assign the button to a variable and reference that va ...

Exploring Angular2: The Router Event NavigationCancel occurring prior to the resolution of the Route Guard

My application has routes protected by an AuthGuard that implements CanActivate. This guard first checks if the user is logged in and then verifies if certain configuration variables are set before allowing access to the route. If the user is authenticated ...

limitation on pairings of two generic types

How can I specify in TypeScript that "You can pass in any two objects, as long as their combination satisfies type X"? For example, consider the following function: function myFunction(options: {x: number, y: string}){ } Now, let's say we have anot ...

Angular 9 - Issue with property assignment on undefined object

After recently upgrading to Angular 9, we encountered an issue during run time. Disabling Ivy resolved the build blocking problem and everything now builds successfully. However, we are facing the following error at run time: Cannot set property "/sites/3 ...

Tips for customizing the CSS of a child component that has been imported from a different module in Angular 2

I have been using agm-snazzy-window from amg-snazzy-window There is a mention that I can modify the css class, but when I try to do so, it doesn't seem to work. Here is an example: map-component.html <agm-snazzy-info-window [closeWhenOthersOp ...

Eliminate JSON data that pertains to dates that are either in the past or future

I am working on integrating upcoming classes and past classes components into my application. I have successfully stored the schedule of classes and can retrieve them using backend services. However, I need to display only the upcoming classes in one compo ...

Why does my test in Angular 2 e2e protactor fail when I do not include browser.sleep()?

While working on my e2e tests, I encountered an issue with a custom select dropdown component I created. When trying to select items in the dropdown, I found that I had to use browser.sleep(...) in my test for it to pass. If I don't include this sleep ...

How can I retrieve the value of an array using ngForm in Angular 2?

One concrete example is a component I created that looks like this. @Component({ selector: 'home', template: ` <form (ngSubmit)="onSubmit(f)" #f="ngForm"> <input type="text" ngControl="people"> ...

incorrect indexing in ordered list

I am facing an issue with the ngIf directive in Angular. My objective is to create a notification system that alerts users about any missing fields. Here's a stackblitz example showcasing the problem: https://stackblitz.com/edit/angular-behnqj To re ...

At what point do we employ providers within Angular 2?

In the Angular 2 documentation, they provide examples that also use HTTP for communication. import { HTTP_PROVIDERS } from '@angular/http'; import { HeroService } from './hero.service'; @Component({ selector: 'my-toh&ap ...

retrieve the preferred item data from the local storage

Having trouble retrieving favorite items from my list item app using Ionic 4. Although my JSON data is added to local storage, I am unable to retrieve the data properly. favourite.service.ts getAllFavoriteItems(){ return this.storage.get("object"); ...

Encountering an "ionic 4 frame-ancestors *" error while attempting to watch a Twitter video

Currently, I am in the process of developing a news app using Ionic 4. I recently tackled the challenge of embedding tweets in Twitter cards successfully. However, a new issue has arisen. When a tweet includes a Youtube video, everything works perfectly ac ...