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就把你的单个模块塞进程序集里面去了.当然,对你的编程也不会造成太大的影响.但有些东西你最好还是知道比较好 ...
随机推荐
- Oracle创建表及管理表
转自:https://www.linuxidc.com/Linux/2018-05/152566.htm 1. Oracle表的创建及管理 创建表包括三个要素,表名,列名,数据类型.每个表都有对应 ...
- Linux 目录栈及目录切换
使用 cd - (中杠)会切换到上一次的目录 cd 命令会改变目录栈 1.dirs 1)功能显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录) 2)语法(1)格式:dirs ...
- wed开发基础--练习题
一.HTML部分 本小节重点: 熟练使用div+span布局,知道div和span的语义化的意思 熟悉对div.ul.li.span.a.img.table.form.input标签有深刻的认知, 初 ...
- Delphi 集合和字符串互转
集合和字符串互转 集合: type TSocketState = (ssDisconnecting, ssDisconnected, ssConnected, ssConnecting, s ...
- js Json数组的增删改查
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- 微信、陌陌等著名IM软件设计架构详解(转)
对微信.陌陌等进行了分析,发出来分享一下(时间有些久了) 电量:对于移动设备最大的瓶颈就是电量了.因为用户不可能随时携带电源,充电宝.所以必须考虑到电量问题.那就要检查我们工程是不是有后台运行,心跳包 ...
- Django后台邮箱配置
Django可以通过发送邮件的方式找回密码,具体细节可以看Django的文档,这里只介绍在settings.py中如何正确进行邮箱的相关配置. 网上很多教程都说了需要在settings.py里添加如下 ...
- C# winform进度条 (异步)
进度条页面: http://www.cnblogs.com/Deckard/archive/2009/06/24/1510451.html //============================ ...
- Android Bug:Error:com.android.dex.DexException: Multiple dex files define Landroid/support/design/widget/CoordinatorLayout$LayoutParams;
项目编译通过,运行时出现异常: Error:com.android.dex.DexException: Multiple dex files define Landroid/support/desig ...
- mybatis实现一对多连接查询
问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...