项目结构

一 创建特性模块,及其包含的组件、服务。

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 )的更多相关文章

  1. Angular惰性加载的特性模块

    一:Angular-CLI建立应用 cmd命令:ng new lazy-app --routing    (创建一个名叫 lazy-app 的应用,而 --routing 标识生成了一个名叫 app- ...

  2. angular学习(五)-- Module

    1.5 模块:Module 模块定义了一个应用程序 模块是应用程序中不同部分的容器 模块是应用控制器的容器 控制器通常属于一个模块 ng 中模块的引入最重要的目的就是为了解决原来全局定义的控制器污染的 ...

  3. angular学习笔记(十五)-module里的'服务'

    本篇介绍angular中的模块:module 在笔记(二)http://www.cnblogs.com/liulangmao/p/3711047.html里已经讲到过模块,这篇主要讲模块的 '服务' ...

  4. Angular pagination分页模块 只提供分页参数处理 不处理分页记录数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 前端学习 node 快速入门 系列 —— 模块(module)

    其他章节请看: 前端学习 node 快速入门 系列 模块(module) 模块的导入 核心模块 在 初步认识 node 这篇文章中,我们在读文件的例子中用到了 require('fs'),在写最简单的 ...

  6. Java 9 的模块(Module)系统

    Java 的模块在Java 9中正式实装,一直没时间来研究一下这个东西,今天就和大家一起学习一下这个功能. Java模块解决了什么问题 最近很多同学问我,胖哥,该怎么学习?该学习什么?这里胖哥也穿插说 ...

  7. Axis2(9):编写Axis2模块(Module)

    Axis2可以通过模块(Module)进行扩展.Axis2模块至少需要有两个类,这两个类分别实现了Module和Handler接口.开发和使用一个Axis2模块的步骤如下: 1. 编写实现Module ...

  8. 多模块项目Module must not contain source root. The root already belongs to module

    多模块项目Module "*" must not contain source root *. The root already belongs to module "* ...

  9. 程序集(Assembly)和模块(Managed Module)

    前言 一直都用集成开发坏境(IDE),一直对模块和程序集的概念理解的不是很直观,因为一Build就把你的单个模块塞进程序集里面去了.当然,对你的编程也不会造成太大的影响.但有些东西你最好还是知道比较好 ...

随机推荐

  1. elk-Logstash

    ELK是三个工具的集合,Elasticsearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构,很多公司利用它来搭建可视化的海量日志分析平台. 2. Logs ...

  2. UI5-学习篇-12-Eclipse同步ABAP服务器UI5应用

    如何在ABAP服务器上部署和运行SAPUI5应用程序 https://blogs.sap.com/2013/06/15/how-to-deploy-and-run-sapui5-application ...

  3. vue.js 作一个用户表添加页面----初级

    使用vue.js 制作一个用户表添加页面,实际上是把原来需要使用js写的部分,改写成vue.js的格式 首先,想象一下,先做思考,我们要添加用户表,设涉及到哪些数据,一个是用户id,一个是用户名,一个 ...

  4. 1.加快Xshell客户端连接到CentOS的速度

    1.编辑打开ssh的配置文件 /etc/ssh/sshd_config 找到里面的UseDNS yes修改为:#UseDNS no service sshd restart

  5. UnityHub破解

    1.退出UnityHub,安装好nodejs执行以下命令 npm install -g asar 2.打开UnityHub安装目录如 C:\Program Files\Unity Hub\resour ...

  6. java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor

    Hibernate4.x与spring3.x整合,有关事务的处理,用Junit4测试,出现org.springframework.beans.factory.BeanCreationException ...

  7. centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod

    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod ...

  8. TCP/IP网络协议的通俗理解,SOCKET,HTTP,SOAP

    TCP/IP,HTTP,SOAP等协议之区别   术语TCP/IP代表传输控制协议/网际协议,指的是一系列协议.“IP”代表网际协议,TCP和UDP使用该协议从一个网络传送数据包到另一个网络.把IP想 ...

  9. 如何遍历List对象

    for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用. System.out.println(str); } .普通for循环 ...

  10. MySQL字符集介绍及配置

    目录 1.MySQL编码集 2.修改字符集 3.MySQL数据库中字符集转换流程 4.修改现有字符集 1.MySQL编码集 查看MySQL支持的字符集 mysql> show character ...