内容简介:Angular 4 依赖注入教程之二 组件中注入服务
阅读须知
本系列教程的开发环境及开发语言:
基础知识
如何创建 Angular 组件
在 Angular 中我们通过以下方式创建一个简单的组件:
@Component({ selector: 'app-root', template: ` <h1>{{title}}</h1> ` }) export class AppComponent { title: string = 'App Works'; }
如何创建 Angular 服务
在 Angular 中我们通过以下方式创建一个简单的服务:
export class DataService { getData() { return ['Angular', 'React', 'Vue']; } }
组件中注入服务
介绍完基础知识,接下来我们来创建一个新的组件 - HeroComponent
,它用来显示英雄的信息,具体实现如下:
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-hero', template: ` <ul> <li *ngFor="let hero of heros"> ID: {{hero.id}} - Name: {{hero.name}} </li> </ul> ` }) export class HeroComponent implements OnInit { heros: Array<{ id: number; name: string }>; ngOnInit() { this.heros = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' } ]; } }
在 HeroComponent
组件中,我们在 ngOnInit
钩子中进行数据初始化,然后利用 ngFor
指令来显示英雄列表的信息。创建完 HeroComponent
组件,我们要来验证一下该组件的功能。首先在 AppModule
中导入 HeroComponent
组件,具体如下:
import { HeroComponent } from './hero/hero.component'; @NgModule({ declarations: [ AppComponent, HeroComponent ], ... }) export class AppModule { }
然后更新一下 AppComponent
组件,具体如下:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <app-hero></app-hero> ` }) export class AppComponent {}
如果不出意外的话,访问 http://localhost:4200/
页面,您将看到如下信息:
ID: 11 - Name: Mr. Nice ID: 12 - Name: Narco ID: 13 - Name: Bombasto ID: 14 - Name: Celeritas ID: 15 - Name: Magneta
难道一切就这么结束了,No! No!别忘记了我们这节课的主题是介绍如何在组件中注入服务。在目前的 HeroComponent
组件,我们的英雄列表信息是固定的,在实际的开发场景中,一般需要从远程服务器获取相应的信息。但我们暂不考虑这个问题,假设另外一个组件也需要利用同样的英雄列表信息,那我们要怎么办,难道直接上 "终极绝招" - Copy && Paste
。当然这是 "终极绝招",岂能随便使用 (不怕被群殴的话,请自便哈)。
针对上面提到的问题,理想的方式是创建一个 HeroService
服务,从而实现数据共享。说干就干,我们马上来创建 HeroService
服务,具体如下:
export class HeroService { heros: Array<{ id: number; name: string }> = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' } ]; getHeros() { return this.heros; } }
在 HeroService
服务中,我们定义了一个 heros
属性和一个 getHeros()
方法:
-
heros - 用于保存英雄的列表信息
-
getHeros() - 用于获取英雄的列表信息
创建完 HeroService
服务后,接下来我们来介绍如何在组件中使用 HeroService
服务。
组件中使用 HeroService
组件中使用 HeroService
服务,主要分为三个步骤:
-
导入
HeroService
服务
import { HeroService } from '../hero.service';
-
声明
HeroService
服务
@Component({ selector: 'app-hero', ... providers: [HeroService] })
-
注入
HeroService
服务
export class HeroComponent implements OnInit { constructor(private heroService: HeroService) { } }
完整代码如下:
import { Component, OnInit } from '@angular/core'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero', template: ` <ul> <li *ngFor="let hero of heros"> ID: {{hero.id}} - Name: {{hero.name}} </li> </ul> `, providers: [HeroService] }) export class HeroComponent implements OnInit { constructor(private heroService: HeroService) { } heros: Array<{ id: number; name: string }>; ngOnInit() { this.heros = this.heroService.getHeros(); } }
看到 providers: [HeroService]
这一行,相信有一些读者会有一些困惑,因为他们可能是按照下面的方式去配置 HeroService
服务。
@NgModule({ declarations: [ AppComponent, HeroComponent ], ... providers: [HeroService], bootstrap: [AppComponent] }) export class AppModule { }
当然两种方式不会影响,我们最终要实现的功能,但这两种方式肯定是有区别的,希望有兴趣的读者,去思考一下哈。在多数场景下,推荐在 NgModule
的 Metadata 信息中配置相应的服务。
我有话说
为什么配置完 HeroService
,在 HeroComponent
组件类的构造函数中还得进行类型声明?
import { HeroService } from '../hero.service'; export class HeroComponent implements OnInit { constructor(private heroService: HeroService) { } }
其实在 @NgModule({...})
或 @Component({...})
Metadata 中我们只是配置 Provider
的相关信息,即告诉 Angular DI (依赖注入) 系统,如何创建根据配置的 provider
信息,创建相应的依赖对象。而在 HeroComponent
组件类中,我们通过 构造注入
的方式去告诉 Angular DI 系统,我们需要的依赖对象类型。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- JoomlaV3.7核心组件无需登录SQL注入漏洞
- ESLint 组件库的 npm 帐号被盗,并被注入恶意代码
- 服务端注入之Flask框架中服务端模板注入问题
- 服务器端电子表格注入 - 从公式注入到远程代码执行
- SQL注入测试技巧TIP:再从Mysql注入绕过过滤说起
- 手机抓包+注入黑科技HttpCanary——最强大的Android抓包注入工具
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
JavaScript Patterns
Stoyan Stefanov / O'Reilly Media, Inc. / 2010-09-21 / USD 29.99
What's the best approach for developing an application with JavaScript? This book helps you answer that question with numerous JavaScript coding patterns and best practices. If you're an experienced d......一起来看看 《JavaScript Patterns》 这本书的介绍吧!