简体   繁体   English

如何使用服务在app.module上一次初始化变量,然后在其他组件中使用它?

[英]How to initialize a variable once on the app.module using a service and use it later in other components?

I have a list of objects to be generated once in the application startup from a backend API using a service, the question is how to initialize that list and access it from the other components? 我必须从后端API使用服务的应用程序启动一次生成的对象列表,问题是如何初始化与其他组件列表和访问它?

For example, in app.module.ts, I want something like: 例如,在app.module.ts中,我想要类似以下内容:

    export class AppComponent {
  title = 'General Title';
  myListFromServiceApi:[DataType]; 
  }

How to call this in mycomponent.ts 如何在mycomponent.ts中调用它

ngOnInit() {
this.myListFromServiceApi= ??
}

Edit 编辑

I already have the service, but I want to avoid invoking the service every time the component added to a view, ie call the service once in the lifetime of the application if that is possible, how can I achieve that? 我已经拥有该服务,但是我想避免每次将组件添加到视图时都调用该服务,即,如果可能的话,在应用程序的生命周期中调用一次服务,我该如何实现?

You could use the rx operator share that prevents a sideeffect (http request in your case) from being executed multiple times on subscription of multiple subscribers and return an observable that's piped through a share from a service, ie DataTypeService : 您可以使用rx运算符share来防止在订阅多个订阅者时多次执行副作用(在您的情况下为http请求),并返回通过服务share (即DataTypeService通过管道传递的可观察对象:

export class DataTypeService {

  dataTypes$: Observable<DataType>;

  constructor(private http: HttpClient) {
    this.dataTypes$ = this.http.get('/api/datatypes').pipe(share());
  }

  getDataTypes() {
    return this.dataTypes$;
  }
}

In your components you could then inject the DataTypeService , subscribe to the observable returned by getDataTypes() and the request would only be executed once. 然后,您可以在组件中注入DataTypeService ,订阅由getDataTypes()返回的getDataTypes()并且该请求将仅执行一次。

constructor(private dataTypeService: DataTypeService){
   this.dataTypeService.getDataTypes().subscribe(dataTypes => {
       this.myListFromServiceApi = dataTypes;
   })
}

If you only wanna work with those data types in your template you could also directly store the observable returned by getDataTypes() on your component and utilize the async pipe 如果只想在模板中使用这些数据类型,则还可以将getDataTypes()返回的observable直接存储在组件中,并利用async管道

See this stackblitz . 看到这个堆叠闪电战 Generally you have one sharedService. 通常,您只有一个sharedService。 That will request the data once and store it as a local variable. 这将请求一次数据并将其存储为局部变量。 Two other components (first and second) only Inject the service in the constructor and keep a reference to the member variable inside a sharedService. 仅其他两个组件(第一个和第二个)将服务注入构造函数中,并在sharedService中保留对成员变量的引用。 This way http call goes out only once. 这样,http调用只会发出一次。 Invoked in: 调用于:

  ngOnInit() {
    this.sharedService.getDataOnce();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 angular 中的 app.module 调用服务内的方法并使用 Imports 中的值 - How to call a method inside a service from app.module in angular and use the values in Imports 如何在我的 app.module 中访问 const(不导出 const 变量)变量并从 angular 中的子组件访问它? - How to access const(without export const variable) variable in my app.module and access it from children components in angular? 如何在app.module中的服务内部调用方法-Angular - How to call a method inside of a service in app.module - Angular 如何在app.module中指定自定义指令到处使用? - how to use a custom directive everywhere by specifying it in app.module? app.module 文件中缺少服务实例 - lack of service instance in app.module file Angular CLI 6. 不能在 app.module 的子模块中使用自定义库组件 - Angular CLI 6. Can't use custom library components in child modules of app.module 在其他多个延迟加载模块之间共享延迟加载(不在 App.module 中)服务(DI 上下文)? - Share lazy-loaded (not in App.module) service (DI context) between other multiple lazy loaded modules? 我的模块没有看到来自app.module的组件 - My module doesn't see components from app.module 在main.ts和app.module中使用相同的服务实例化(到处都是单个) - Use the same service instantiation from main.ts and in app.module (singleton everywhere) 无法通过app.module在视图中访问angular4组件 - angular4 Components not accessible in view through app.module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM