Angular 创建CacheService的新实例

Angular 创建CacheService的新实例,angular,typescript,Angular,Typescript,我将CacheService与包含许多下拉值的搜索屏幕一起使用。它只在为一个下拉列表设置缓存时起作用,如果为多个下拉列表设置缓存则不起作用,即使它们具有不同的键。我想我需要创建一个缓存服务的新实例 例如: constructor(private _appParams: AppParamasService,private _cacheService: CacheService) { } getUserLocations(ID: string, allList: string) {

我将CacheService与包含许多下拉值的搜索屏幕一起使用。它只在为一个下拉列表设置缓存时起作用,如果为多个下拉列表设置缓存则不起作用,即使它们具有不同的键。我想我需要创建一个缓存服务的新实例

例如:

constructor(private _appParams: AppParamasService,private _cacheService: CacheService) {
}

getUserLocations(ID: string, allList: string) {
        let userLocationsCache: string;

        let res: any | null = this._cacheService.get(userLocationsCache);
        if (res) {
            this.results = res;

        }
        else {

            this._appParams.getUserLocations(ID, allList)
                .subscribe(
                data => {
                    this.results = data.result;
                    this._cacheService.set(userLocationsCache, data.result);    

                },
                error => this.showMessage(' Method: ' + <any>error, 0));


        }
}

    getAllStatuses() {
        let statusCache: string="";

        let res1: any | null = this._cacheService.get(statusCache);
        if (res1) {
            this.iStatuses = res1;
                  }
        else {

            this._appParams.getStatuses()
                .subscribe(
                data => {
                    this.iStatuses = data.result;
                    this._cacheService.set(statusCache, data.result);    
                },
                error => this.showMessage(' Method: ' + <any>error, 0));

        }
}

如果您只是将服务(或任何服务)注入构造函数中,Angular将遍历组件/模块树并查找服务的提供者。您很可能已将服务添加到模块的提供者数组中。这将产生一个单例服务,在您将该服务注入该模块的任何地方都可以使用该服务

如果确实希望为组件使用独立的服务实例,可以将服务添加到组件的提供者数组中,如下所示:

@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: [ './my-comppnent.component.css' ],
  providers: [CacheService]
})
export class MyComponent {
...

如果你在app.module.ts中将CacheService公开为提供商,是的,我将其取出..查看我更新的上述代码…注意,如果我只在一个下拉列表中使用CacheService,它就可以工作。你可以发布CacheService的代码吗?请删除该服务,它仍然不工作..相同的错误..更新了我的代码上面我不知道你的组件的确切结构,但我认为你的下拉元素需要是独立的组件,每个组件都有自己的CacheService实例(通过组件提供程序)bu在您的示例中,下拉列表是一个单独的组件,而不是将下拉列表与所有其他控件一起放在一个组件中。不确定这是否适合我的项目。如果您想为每个下拉列表获取单独的服务实例,我确实认为这是一种方法。我实际上认为,将视图分解为小的可重用视图组件是一件好事,它使应用程序不那么复杂,但这更像是一种观点。注入服务的多个实例也应该有效,为什么您希望在这里出现性能问题?
import { environment } from 'environments/environment';

@Injectable()

export class AppParamasService {

    constructor(private _http: Http) {

    }

 getStatuses() {
         return this._http.get(environment.BASE_API_URL + 'GetAllStatuses/')
             .map((response: Response) => response.json())
             .catch(this.handleError);

     }
 getUserLocations(a: string, allList: string) {
         return this._http.get(environment.BASE_API_URL + 'GetUserLocations/' + a + '/' + allList)
             .map((response: Response) => response.json())
             .do(data => console.log('loctions ' + JSON.stringify(data)))
             .catch(this.handleError);
     }
@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: [ './my-comppnent.component.css' ],
  providers: [CacheService]
})
export class MyComponent {
...