Angular2共享HTTP服务

万切霍

我已经阅读了一些有关在组件之间共享服务的知识,以及使用app组件的基本思想,据我了解,本质上是将服务的一个实例创建为提供者。

我加载了具有嵌套组件的组件,并且嵌套组件都使用此共享服务。页面上将来会触发一个事件,现在我需要HTTP服务来更新和更新所有嵌套组件模板元素。我究竟如何“强制”此更新?

另外,这是否意味着因为我在app组件中共享了一项服务,所以每当页面“根”组件加载时,HTTP服务便会运行?

海梅·斯蒂尔(Jaime Still)

更新:这个周末我没有时间整理任何东西,但是如果情况仍然不清楚,我做了一个简化的示例,以展示服务注入在Angular 2中的工作方式

AppComponent在@Component装饰器中将AppService列为提供程序,这意味着将在此组件级别注入服务的单例。在ChildComponent中,不需要将服务列出为提供程序,因为它将使用注入到AppComponent中的相同实例。它需要做的就是导入AppService模块,然后将服务注入到构造函数定义中。

相反,IsolatedComponent使用一个单独的AppService实例,因此它确实通过@Component装饰器中的providers数组注入了一个新的单例实例。IsolatedChildComponent将使用与IsolatedComponent相同的服务实例,因此与ChildComponent一样,它所需要做的就是导入AppService模块,并将该实例注入其构造函数定义中。

请注意,每当初始化组件时,每个组件如何更新共享的绑定属性,消息,并且子组件自动捕获这些更新。可以将相同的逻辑应用于进行API调用的服务。

这是服务和组件的代码:

app.service.ts

import { Injectable } from '@angular/core';

@Injectable()
export class AppService {
  messages: string[] = [];

  updateMessages(msg: string) {
    this.messages.push(msg);
  }
}

app.component.ts

import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
import { ChildComponent } from './child.component';
import { IsolatedComponent } from './isolated.component';

@Component({
  selector: 'my-app',
  template: `
  <h1>AppComponent Tree</h1>
  <p>
    AppComponent and ChildComponent consume the same instance of AppService
  </p>
  <child-component></child-component>
  <hr />
  <isolated-component></isolated-component>
  `,
  providers: [AppService],
  directives: [ChildComponent, IsolatedComponent]
})
export class AppComponent implements OnInit {
  messages: string[];

  constructor(private appService: AppService) {
    this.messages = appService.messages;
  }

  ngOnInit() {
    this.addMessage(`AppComponent Initialized`);
  }

  private addMessage(msg: string) {
    this.appService.updateMessages(msg);
  }
}

child.component.ts

import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';

@Component({
  selector: 'child-component',
  template: `
  <div *ngFor="let message of messages">{{message}}</div>
  `
})
export class ChildComponent implements OnInit {
  messages: string[];

  constructor(private appService: AppService) {
    this.messages = appService.messages;
  }

  ngOnInit() {
    this.addMessage(`ChildComponent Initialized`);
  }

  private addMessage(msg: string) {
    this.appService.updateMessages(msg);
  }
}

孤立的组件

import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
import { IsolatedChildComponent } from './isolated-child.component';

@Component({
  selector: 'isolated-component',
  template: `
  <h1>Isolated Component Tree</h1>
  <p>
    IsolatedComponent and IsolatedChildComponent consume an 
    instance of AppService separate from the AppComponent tree
  </p>
  <isolated-child></isolated-child>
  `,
  providers: [AppService],
  directives: [IsolatedChildComponent]
})
export class IsolatedComponent implements OnInit {
  messages: string[];

  constructor(private appService: AppService) {
    this.messages = appService.messages;
  }

  ngOnInit() {
    this.addMessage(`IsolatedComponent initialized`);
  }

  private addMessage(msg: string) {
    this.appService.updateMessages(msg);
  }
}

isolated-child.component.ts

import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';

@Component({
  selector: 'isolated-child',
  template: `
  <div *ngFor="let message of messages">{{message}}</div>
  `
})
export class IsolatedChildComponent implements OnInit {
  messages: string[];

  constructor(private appService: AppService) {
    this.messages = appService.messages;
  }

  ngOnInit() {
    this.addMessage(`IsolatedChildComponent initialized`);
  }

  private addMessage(msg: string) {
    this.appService.updateMessages(msg);
  }
}

请参阅“分层注射器”文档。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular2共享HTTP服务

来自分类Dev

Angular2简单的Http服务注入

来自分类Dev

Angular2简单的Http服务注入

来自分类Dev

Angular2中的HTTP服务错误

来自分类Dev

测试angular2 http服务

来自分类Dev

使用共享服务更改angular2中的检测

来自分类Dev

在angular2中编写共享服务

来自分类Dev

在Angular2服务中多次发出HTTP请求

来自分类Dev

Angular2:Web服务/ http请求的代码组织

来自分类Dev

将Http注入服务时Angular2 [typescript]错误

来自分类Dev

扩展Angular2 Http不会注入服务

来自分类Dev

Angular2:无法订阅从共享服务发出的自定义事件

来自分类Dev

通过共享单例服务进行Angular2组件通信

来自分类Dev

在 Angular2 中使用事件发射器在服务和组件之间共享数据

来自分类Dev

Angular2:注入服务

来自分类Dev

Angular2:注入服务

来自分类Dev

Angular2共享组件与动态不共享组件

来自分类Dev

Angular2 HTTP同步

来自分类Dev

Angular2 Http错误

来自分类Dev

从组件之间的HTTP服务获取的Angular 2共享/更改数据

来自分类Dev

Angular 2+ 与所有 http 服务共享 json 配置值

来自分类Dev

Angular 2跨组件共享Websocket服务

来自分类Dev

Angular 2-共享服务的实现

来自分类Dev

Angular 2-使用共享服务

来自分类Dev

angular 2 通过服务共享数组

来自分类Dev

Angular2 Http与HTTP_PROVIDERS

来自分类Dev

Angular2 http.post表单值到php服务器

来自分类Dev

使用Angular2的服务,在组件方法中进行HTTP调用

来自分类Dev

使用Angular2和TypeScript将http功能移到其自己的服务中