Angular之特性模块 ( Feature Module )
项目结构

一 创建特性模块,及其包含的组件、服务。
ng g module art
ng g component art/music
ng g component art/dance
ng g service art/performance
二 特性模块
art.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MusicComponent } from './music/music.component';
import { DanceComponent } from './dance/dance.component';
import { PerformanceService } from './performance.service';
@NgModule({
imports: [
CommonModule
],
// 把特性模块和组件关联起来:添加专属于这个模块的可声明对象(组件、指令和管道)
declarations: [MusicComponent, DanceComponent],
// 导出组件:导入特性模块后,就可以使用其中包含的组件模版了
exports: [
MusicComponent,
DanceComponent
],
providers: [
PerformanceService
]
})
export class ArtModule { }
三 组件
music.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-music',
templateUrl: './music.component.html',
styleUrls: ['./music.component.css']
})
export class MusicComponent implements OnInit {
@Input()
name: string;
constructor() { }
ngOnInit() {
}
}
music.component.html
<p>
music works!{{name}}
</p>
dance.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-dance',
templateUrl: './dance.component.html',
styleUrls: ['./dance.component.css']
})
export class DanceComponent implements OnInit {
@Input()
name: string;
constructor() { }
ngOnInit() {
}
}
dance.component.html
<p>
dance works!{{name}}
</p>
四 服务
performance.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class PerformanceService {
constructor() { }
perform(): string[] {
return ['唱歌', '跳舞'];
}
}
五 导入特性模块
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ArtModule } from './art/art.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ArtModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
六 使用特性模块的组件、服务
app.component.ts
import { Component } from '@angular/core';
import { PerformanceService } from './art/performance.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(public performance: PerformanceService) {
}
perform() {
console.log(this.performance.perform());
}
}
app.component.html
<p>
app works!
</p>
<!-- 单向绑定:组件的属性 -->
<app-music [name]="'水边的阿狄丽娜'"></app-music>
<!-- 单向绑定:值是固定的,故使用一次性绑定 ( a "one-time" binding ) -->
<app-dance name="踢踏舞"></app-dance>
<button type="button" (click)="perform();">演出</button>
七 运行效果

Angular之特性模块 ( Feature Module )的更多相关文章
- Angular惰性加载的特性模块
一:Angular-CLI建立应用 cmd命令:ng new lazy-app --routing (创建一个名叫 lazy-app 的应用,而 --routing 标识生成了一个名叫 app- ...
- angular学习(五)-- Module
1.5 模块:Module 模块定义了一个应用程序 模块是应用程序中不同部分的容器 模块是应用控制器的容器 控制器通常属于一个模块 ng 中模块的引入最重要的目的就是为了解决原来全局定义的控制器污染的 ...
- angular学习笔记(十五)-module里的'服务'
本篇介绍angular中的模块:module 在笔记(二)http://www.cnblogs.com/liulangmao/p/3711047.html里已经讲到过模块,这篇主要讲模块的 '服务' ...
- Angular pagination分页模块 只提供分页参数处理 不处理分页记录数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 前端学习 node 快速入门 系列 —— 模块(module)
其他章节请看: 前端学习 node 快速入门 系列 模块(module) 模块的导入 核心模块 在 初步认识 node 这篇文章中,我们在读文件的例子中用到了 require('fs'),在写最简单的 ...
- Java 9 的模块(Module)系统
Java 的模块在Java 9中正式实装,一直没时间来研究一下这个东西,今天就和大家一起学习一下这个功能. Java模块解决了什么问题 最近很多同学问我,胖哥,该怎么学习?该学习什么?这里胖哥也穿插说 ...
- Axis2(9):编写Axis2模块(Module)
Axis2可以通过模块(Module)进行扩展.Axis2模块至少需要有两个类,这两个类分别实现了Module和Handler接口.开发和使用一个Axis2模块的步骤如下: 1. 编写实现Module ...
- 多模块项目Module must not contain source root. The root already belongs to module
多模块项目Module "*" must not contain source root *. The root already belongs to module "* ...
- 程序集(Assembly)和模块(Managed Module)
前言 一直都用集成开发坏境(IDE),一直对模块和程序集的概念理解的不是很直观,因为一Build就把你的单个模块塞进程序集里面去了.当然,对你的编程也不会造成太大的影响.但有些东西你最好还是知道比较好 ...
随机推荐
- day07-多表查询
本节重点: 多表连接查询 符合条件连接查询 子查询 准备工作:准备两张表,部门表(department).员工表(employee) create table department( id int, ...
- C#与.NET概述
.NET Framework是一个支持生成和运行下一代应用程序和web服务的集成在Windows中的组件. 关键组建为CLR和FCL. 为其运行的应用程序提供各种服务的托管执行环境,简化的开发和部署以 ...
- 01_hello world
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java通过过滤器 设置跨域允许
前人种树:https://www.cnblogs.com/CBDoctor/p/4235082.htmlhttps://blog.csdn.net/yuting0787/article/details ...
- spark Kryo serialization failed: Buffer overflow 错误
今天在写spark任务的时候遇到这么一个错误,我的spark版本是1.5.1. Exception in thread "main" com.esotericsoftware.kr ...
- Spring Cloud限流详解
转自:https://blog.csdn.net/tracy38/article/details/78685707 在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud ...
- 多线程中的join总结笔记
join方法的原理 就是调用相应线程的wait方法进行等待操作的,假如线程1中调用了线程2的join方法,则相当于在线程1中调用了线程2的wait方法,当线程2执行完(或者到达等待时间),线程2会自动 ...
- $event Object angularjs
You can pass the $event object as an argument when calling the function. The $event object contains ...
- [PHP]PHP的session机制,配置与高级应用
---------------------------------------------------------------------------------------------------- ...
- rhel7磁盘管理
一.MBR主引导记录 MBR有512个字节,分为三个部分:第一部分446个字节,存储了引导分区:第二部分64字节为分区表:第三部分2个字节结束符:每个分区需16个字节,所以MBR的模式 ...