Angular 4依赖注入学习教程之组件服务注入(二)
- 作者: 上过小学
- 来源: 51数据库
- 2021-08-08
学习目录
- angular 4 依赖注入教程之一 依赖注入简介
- angular 4 依赖注入教程之二 组件服务注入
- angular 4 依赖注入教程之三 classprovider的使用
- angular 4 依赖注入教程之四 factoryprovider的使用
- angular 4 依赖注入教程之五 factoryprovider配置依赖对象
- angular 4 依赖注入教程之六 injectable 装饰器
- angular 4 依赖注入教程之七 valueprovider的使用
- angular 4 依赖注入教程之八 injecttoken的使用
前言
之前的已经介绍了angular 4 的基础知识,下面将介绍angular 4依赖注入之组件服务注入的相关内容,分享出来供大家参考学习,下面来来看看详细的介绍:
本系列教程的开发环境及开发语言:
- angular 4 +
- angular cli
- typescript
基础知识
如何创建 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 服务,主要分为三个步骤:
1、导入 heroservice 服务
import { heroservice } from '../hero.service';
2、声明 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 系统,我们需要的依赖对象类型。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
