I am implementing caching only for gift card module and I have created http-cache.service and cache interceptor.When I add the service in app.module.ts it works but I need to implement this separately only for giftcard.When I do separately for gift card it doesn't work.This is my code
giftcard-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { GiftcardComponent } from './list/giftcard.component';
import { GiftcardtransactionComponent } from './giftcardtransaction/giftcardtransaction.component';
const routes: Routes = [
{
path: '',
component: GiftcardComponent
},
{
path: 'sales',
component: GiftcardtransactionComponent,
data: { breadcrumb: "Sales" }
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class GiftCardRoutingModule { }
giftcard.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { GiftcardComponent } from './list/giftcard.component';
import { GiftcardtransactionComponent } from './giftcardtransaction/giftcardtransaction.component';
import { GiftCardRoutingModule } from './giftcard-routing.module';
import { CacheInterceptor } from '../helper/interceptors/cache.interceptor';
import { HttpCacheService } from '../helper/services/cache/http-cache.service';
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
@NgModule({
imports: [
CommonModule,
GiftCardRoutingModule,
RouterModule
],
declarations: [
GiftcardComponent,
GiftcardtransactionComponent
],
providers: [
HttpCacheService,{ provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }
],
})
export class GiftCardModule { }
CodePudding user response:
Which error occurs when you reference the service in your module?
Is you module only importet once?
I also identified an issue with your Routing declaration. You already imported RouterModule in your giftcard-routing.module.ts and exported it ther.
You don't need to import Router Module separately in your giftcard.module.ts This can also cause some issues
CodePudding user response:
You can inject UserService anywhere in your application.
The service itself is a class that the CLI generated and that's decorated with @Injectable(). By default, this decorator has a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that Angular should provide the service in the root injector.
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class UserService {
}
It's also possible to specify that a service should be provided in a particular @NgModule. For example, if you don't want UserService to be available to applications unless they import a UserModule you've created, you can specify that the service should be provided in the module:
import { Injectable } from '@angular/core';
import { UserModule } from './user.module';
@Injectable({
providedIn: UserModule,
})
export class UserService {
}