Steps to showcase a visual element using an interceptor

I am in the process of building a web application with Angular 6. One key aspect of this project is implementing an interceptor service that utilizes the interface HttpInterceptor to intercept specific types of HTTP requests. So far, the interceptor class is functioning flawlessly and successfully capturing the intended HTTP calls. Given that there are various graphical components within the application, I am curious about the best approach for displaying a graphic component (e.g. spinner or modal window) by utilizing code from the interceptor.

Let's consider an example:

@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {

    intercept(req: HttpRequest<any>, next: HttpHandler ) {

        let updateReq;

        updateReq = req.clone({
            setParams: {
                responseType: 'no-type'
            }
        });

        console.log(updateReq);

        return next.handle(updateReq).pipe(tap(
            event => console.log(event),
            err => console.log(err)
        ));
    }

}

For instance, if I wanted to check for certain properties within req and then display a graphical component accordingly, how would I go about implementing this across my entire application?

Answer №1

  1. Develop a spinner component with a toggle feature to show or hide the spinner.
  2. Establish a new service called "spinnerservice" that switches the flag and triggers an event when the spinner is toggled.
  3. Integrate your service into both your spinner component and interceptor.
  4. Utilize the "spinnerservice" within the interceptor.
  5. Monitor the event emitted by your service in the spinner component to adjust the spinner's status accordingly.

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

Error encountered during Jasmine unit testing for the ng-redux @select directive

Here is a snippet from my component.ts file: import { Component, OnInit } from '@angular/core'; import { select } from 'ng2-redux'; import { Observable } from 'rxjs/Observable'; import { PersonalDetailsComponent } from ' ...

Is there an Angular counterpart to Vue's <slot/> feature?

Illustration: Main component: <div> Greetings <slot/>! </div> Subordinate Component: <div> Planet </div> Application component: <Main> <Subordinate/> </Main> Result: Greetings Planet! ...

Understanding React and TypeScript Higher Order Components: How to Resolve the Type '{}' is not assignable to Type P Error

While following along with the Learning React with TypeScript book, I encountered a problem where I hit a wall while coding. Despite surrendering and copying code directly from the book, the compiler is still unhappy As a temporary solution, I have resort ...

Why does Prettier choose to omit trailing commas?

After creating a brand new Angular application using ng new test-app, I added Prettier to the devDependencies and successfully installed it. I made sure to disable all extensions in VSCode except for Prettier. The issue arises when I configure VSCode to f ...

Snackbar expands to cover the entire screen upon deployment

The issue with the oversized snackbar only seems to occur in the deployed version, and I'm unable to identify the root cause. I am hoping someone can provide insight on what adjustments can be made in the StackBlitz project to prevent this bug from ap ...

Exploring TypeScript: Determining the data type of an object key within the object

Apologies for the vague title, I'm struggling to articulate my problem which is probably why I can't find a solution! Let me illustrate my issue with a snippet of code: type Type<T> = { key: keyof T, doStuff: (value: T[typeof key]) =& ...

Angular build command failing to create dist directory

I've been struggling with deploying my angular/ionic project to AWS using S3. I am facing an issue where the dist folder is not generated when running either the ng build or ng build --prod command. Despite multiple attempts, I have been unable to res ...

Having trouble with your Router Guard?

Currently, I find myself in a predicament with a CanActivate router guard. The use of these guards is resulting in an undesired reload of the entire page. After implementing two different versions of the guard, I have encountered a behavior that I cannot ...

Angular data binding between an input element and a span element

What is the best way to connect input texts with the innerHTML of a span in Angular6? Typescript file ... finance_fullname: string; ... Template file <input type="text" id="finance_fullname" [(ngModel)]="finance_fullname"> <span class="fullnam ...

Is it possible for ngIf to only hide the div without impacting the overall layout?

I utilize a left side menu and main content. <div class="row"> <div ngIf="loginUser" class="sidebar col-md-3> ....... </div! <div class="main col-md-9> ....... </div> </div> It is hidden when ...

What function does the super() method serve in TypeScript subclasses?

On top of being irritating and requiring modifications to every child class whenever the parent class is updated... ...

Adding new information to a list when a button is clicked: Combining Angular TypeScript with Laravel

Upon entering a reference ID in my system, it retrieves the corresponding record from the database. However, I am facing an issue where adding a new reference number overrides the existing record instead of appending it to the list. https://i.sstatic.net/ ...

Update the object status from being null to a different one using the set state function

class SubCategoriesComponent extends React.Component< SubCategoryStateProps > { constructor(props: RouteComponentProps<CategoryUrlParams>) { super(props); this.state = { category: null, }; } componentDidMount() { ...

The error message "The function XXX.isSupported is not defined" is displayed

Integrating a JavaScript library into Typescript When I use this function in JavaScript, it works perfectly: _clickHandler() { TouchID.isSupported() .then(authenticate) .catch(error => { AlertIOS.alert('TouchID not supported'); }); ...

When defining a component decorator in Angular 2, mixing tabs and spaces can lead to an unhandled exception

After refactoring my Angular 2 component code and adding some tabulations, the code suddenly stopped working. Surprisingly, when I removed the tabs (spaces), it started working again. @Component({ templateUrl : './app.component.html', styl ...

When declaring an array of numbers in sequelize-typescript, it triggers a TypeScript error

In my application, I am working with PostgreSQL, Sequelize, Sequelize-TypeScript, and TypeScript. I have a need for a table called Role where each role has multiple permissions of type integer. I'm following the guidelines provided in the sequelize-ty ...

Tips for passing the actions payload within an ngrx effect to the WiithLatestFrom() function in order to retrieve a specific value from the state

Hey there, struggling to pass my actions payload to getMainCareuserMessagesConversationAppointmentsShift(?) with the following effect. caregiverAcceptFetch = createEffect(() => { return this.actions.pipe( ofType(rdxMainCareuserMessagesConversa ...

Ensuring external library object properties are limited in Typescript

Trying to utilize the notify function from an external library has been a bit challenging. The declaration in the library is as follows: // library.js export declare const notify: { (args: NotificationsOptions | string): void; close(id: unknown): ...

What is the method for utilizing Tuple elements as keys in a Mapped Type or template literal within typescript?

Is there a specific way to correctly type the following function in TypeScript? Assuming we have a function createMap() that requires: a prefix (e.g. foo) and a tuple of suffixes (e.g. ['a', 'b', 'c']) If we call createMap(& ...

What are some ways to optimize the performance of a Select Box?

I am attempting to show a lengthy list of countries in an ion-select. Currently, there are 249 countries that I need to load. Unfortunately, the rendering performance is quite slow on my phone. <ion-list margin-top margin-bottom> <ion-item> ...