了解Angular4中的共享模块
本篇文章带大家了解一下Angular4中的共享模块。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
相关教程推荐:《angular教程》
1. AppModule
@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule ], exports: [ AppComponent ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
imports 本模块声明的组件模板需要的类所在的其它模块。
providers 服务的创建者,并加入到全局服务列表中,可用于应用任何部分。
declarations 声明本模块中拥有的视图类。Angular 有三种视图类:组件、指令和管道。
exports declarations 的子集,可用于其它模块的组件模板。
bootstrap 指定应用的主视图(称为根组件),它是所有其它视图的宿主。只有根模块才能设置 bootstrap 属性。
2. CommonModule
先看一下CommonModule中有什么内容。
common.module.ts代码
@NgModule({ imports: [ NgZorroAntdModule, AngularCommonModule ], declarations: [ CommonComponent, NumberFilterPipe, ButtonDirective, StepComponent ], exports: [ CommonComponent, NumberFilterPipe, ButtonDirective, StepComponent ], providers: [ ], })
我在comon 文件夹中创建了service, pipe, component, directive,但是这个service和这个module没有任何关系。至于service会在下面说到。然后将 pipe, component, directive输出,这样其他模块才能使用。
3. AngularModule
然后我们需要在其他的模块中使用这个模块,就需要import进来。
import { NgModule } from '@angular/core'; import { AngularComponent } from './angular.component'; import {RouterModule, Routes} from '@angular/router'; import {CommonModule as CommonPrivateModule} from '../../common/common.module'; import {CommonModule} from '@angular/common'; import {HttpService} from '../../common/service/http.service'; import {HttpCommonService} from '../../common/service/http-common.service'; import {BrowserModule} from '@angular/platform-browser'; const routes: Routes = [ {path: '', component: AngularComponent} ]; @NgModule({ imports: [ CommonModule, RouterModule.forChild(routes), CommonPrivateModule ], declarations: [AngularComponent], providers: [] }) export class AngularModule { }
因为CommonModule与系统内的module重名,所以我重命名为CommonProvateModule。这样我们就可以在AngularModule中使用共享模块的内容。
angular.component.html
<p> <app-step [stepString]="['common component']"></app-step> <button appButton> common directive</button> <br> common pipe: {{1 | numberFilter}} </p>
这个html文件中我使用了之前创建的 StepComponent, NumberFilterPipe, ButtonDirective。
4. Service
service前面在Common的文件加下,但是没有在CommonModule provide。这是为什么呢,因为service靠Angular 的依赖注入体系实现的,而不是模块体系。如果我们在CommonModule provide,那么我们在各个模块使用的service不是一个实例,而是多个实例。下面我们就来测试一下。
先说一下例子的模块结构, AppModule,HomeModule(AppModule的子模块), AngularModule(HomeModule的子模块)。然后分别在三个模块中引入CommonModule。
修改一下上面的CommonModule,将HttpCommonService 提供出去。
@NgModule({ imports: [ NgZorroAntdModule, AngularCommonModule ], declarations: [ CommonComponent, NumberFilterPipe, ButtonDirective, StepComponent ], exports: [ CommonComponent, NumberFilterPipe, ButtonDirective, StepComponent ], providers: [ HttpCommonService ], })
HttpCommonService
import { Injectable } from '@angular/core'; import {Http, Request, RequestOptions} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {NzMessageService} from 'ng-zorro-antd'; @Injectable() export class HttpCommonService { private testService: number; constructor(public httpService: Http, private _message: NzMessageService) { } set(number) { this.testService = number; } get() { return this.testService; } }
这里在service内部有两个方法,一个用于设置变量testService,一个用于取这个变量。
AppComponent
export class AppComponent implements OnInit { title = 'app'; constructor(private httpCommonService: HttpCommonService) {} ngOnInit(): void { console.log('appmodule 取值之前的number:' + this.httpCommonService.get()); this.httpCommonService.set(1); } }
HomeCompoent
export class HomeComponent implements OnInit { constructor(private httpCommonService: HttpCommonService) { } ngOnInit() { console.log('homemodule 取值之前的number:' + this.httpCommonService.get()); this.httpCommonService.set(2); } }
AngularComponent
export class AngularComponent implements OnInit { firstString: string; constructor(private httpCommonService: HttpCommonService) { } ngOnInit() { console.log('angularmodule 取值之前的number:' + this.httpCommonService.get()); } }
最后看一下控制台的输出:
可以看到service内部的变量每一次都是一个新值。
然后我们在将CommonModule中的service去掉,就是这个公共模块不提供service。然后在将AppModule修改一下,提供HttpCommonService。 我们再看一下页面控制台的输出。
可以看到现在是一个实例,而且service内部的变量也是缓存起来的。
所以对于service我们还是放在模块中去提供,不要放在共享模块中了。
至于页面的模板可以访问angular - blank .
更多编程相关知识,请访问:编程入门!!
以上是了解Angular4中的共享模块的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

本教程向您展示了如何将自定义的Google搜索API集成到您的博客或网站中,提供了比标准WordPress主题搜索功能更精致的搜索体验。 令人惊讶的是简单!您将能够将搜索限制为Y

因此,在这里,您准备好了解所有称为Ajax的东西。但是,到底是什么? AJAX一词是指用于创建动态,交互式Web内容的一系列宽松的技术。 Ajax一词,最初由Jesse J创造

本文系列在2017年中期进行了最新信息和新示例。 在此JSON示例中,我们将研究如何使用JSON格式将简单值存储在文件中。 使用键值对符号,我们可以存储任何类型的

增强您的代码演示:开发人员的10个语法荧光笔 在您的网站或博客上共享代码片段是开发人员的常见实践。 选择合适的语法荧光笔可以显着提高可读性和视觉吸引力。 t

利用轻松的网页布局:8个基本插件 jQuery大大简化了网页布局。 本文重点介绍了简化该过程的八个功能强大的JQuery插件,对于手动网站创建特别有用

本文介绍了关于JavaScript和JQuery模型视图控制器(MVC)框架的10多个教程的精选选择,非常适合在新的一年中提高您的网络开发技能。 这些教程涵盖了来自Foundatio的一系列主题

核心要点 JavaScript 中的 this 通常指代“拥有”该方法的对象,但具体取决于函数的调用方式。 没有当前对象时,this 指代全局对象。在 Web 浏览器中,它由 window 表示。 调用函数时,this 保持全局对象;但调用对象构造函数或其任何方法时,this 指代对象的实例。 可以使用 call()、apply() 和 bind() 等方法更改 this 的上下文。这些方法使用给定的 this 值和参数调用函数。 JavaScript 是一门优秀的编程语言。几年前,这句话可
