(五)Angular4 英雄征途HeroConquest-多组件开发
来源:互联网 发布:淘宝比较靠谱的韩代 编辑:程序博客网 时间:2024/05/22 02:14
(五)Angular4 英雄征途HeroConquest-多组件开发
多组件开发:将组建拆分,成多个组建,想成一父多子的架构,便于大型项目的维护与开发以及迭代。
父组件app.component.ts文件
import { Component } from '@angular/core';// 引入子组件hero-detailimport { HeroDetailComponent} from './hero-detail.component';// 引入非组件的模块化属性import { HeroArray } from './herodata';import { HeroProperty} from './heroproperty';@Component({// @Component装饰器为组件提供了Angular元数据。// CSS选择器的名字app-hero-detail会匹配元素的标签名,用于在父组件的模板中标记出当前组件的位置。// template后面,我们会把<app-hero-detail>添加到AppComponent的模板中。// 这就是所谓的自定义标签 selector: 'app-root', template: `<h1>{{title}}</h1><!--上一节代码begin--> <br> <h2> -------{{from}}</h2> <ul> <li> <div><label>ID:</label>{{hero.id}} <label> name:</label>{{hero.name}}</div> </li> </ul> <div> <label>name: </label> <input [(ngModel)]="hero.name" > <label>ID: </label> <input [ngModel]="hero.id" (ngModelChange)="hero.id=$event"></div><div><ul class="heroes"><li *ngFor="let onehero of Heros" (click)="onSelect(onehero)" [class.selected]="onehero===selectedHero"><span class="badge">ID:{{onehero.id}}</span> {{onehero.name}}</li></ul></div ><!--上一节代码end--><!--本节主要改动的代码begin--><app-hero-detail [DetailHero]="selectedHero"></app-hero-detail><!--此处将selectedHero的值通过DetailHero传入到子组件hero-detail中去--><!--此处的app-hero-detail标签便是子组件的Component装饰器下的元数据的selector属性的定义--><!--本节主要改动的代码end--> ` , styleUrls: ['./app.component.css']})export class AppComponent { title = 'My conquest is the sea of stars.'; from = 'Reinhard von Lohengramm'; hero: HeroProperty = { id: 9527, name: 'Lee', }; Heros = HeroArray; selectedHero: HeroProperty; onSelect(each: HeroProperty): void { this.selectedHero = each; }}
子组件hero-detail.component.ts文件
//导入 Input装饰器import { Component, Input } from '@angular/core';//导入 HeroPropertyimport { HeroProperty} from './heroproperty';// @Component装饰器为组件提供了Angular元数据。// CSS选择器的名字hero-detail会匹配元素的标签名,// 用于在父组件的模板中标记出当前组件的位置。// 后面我们会把<hero-detail>添加到AppComponent的模板中。@Component({ selector: 'app-hero-detail', <!--在父组件以标签的形式引入子组件的标签字段--> template: ` <!--herodetail--><div *ngIf="DetailHero"><h2>{{DetailHero.name}} details!</h2><label>id: </label>{{DetailHero.id}}<div> <label>name: </label> <input [(ngModel)]="DetailHero.name" placeholder="name"/></div></div><!--herodetail--> `,})export class HeroDetailComponent { @Input() DetailHero: HeroProperty; // 利用Input装饰器来表明 DetailHero是父组件输入过来的,赋有输入属性}
herodata.ts文件
import { HeroProperty} from './heroproperty';export const HeroArray: HeroProperty[] = [ { id: 1, name: 'Asimov' }, { id: 2, name: 'IronMan' }, { id: 3, name: 'Gen' }, { id: 4, name: 'AnglovLee' }];//export 是可导出的声明关键字,const是声明块级作用于可见的变量属于ES2016规范//ES2015用的是let更早用的是var
heroproperty.ts
export class HeroProperty { id: number; name: string;}
app.module.ts
要在此处声明引入子组件,把HeroDetailComponent添加到该模块的declarations数组中。
import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import { FormsModule } from '@angular/forms';import { AppComponent } from './app.component';import { HeroDetailComponent} from './hero-detail.component';@NgModule({ declarations: [ AppComponent, HeroDetailComponent //要在此处声明 ], imports: [ BrowserModule, FormsModule, ], providers: [], bootstrap: [AppComponent]})export class AppModule { }
效果图
阅读全文
0 0
- (五)Angular4 英雄征途HeroConquest-多组件开发
- (三)Angular4 英雄征途HeroConquest-初始化组件文件介绍
- (二)Angular4 英雄征途HeroConquest-编辑英雄
- (四)Angular4 英雄征途HeroConquest-主从结构
- (六)Angular4 英雄征途HeroConquest-服务service
- (一)Angular4 英雄征途HeroConquest-Angular4的安装
- angular4 英雄编辑器
- 使用Angular4.0开发星级评价组件
- Angular4-在线竞拍应用-开发组件
- Hibernate征途(五)之继承映射和组件映射
- Angular4.0分页组件
- angular4----组件间通信
- Angular4 组件生命周期钩子
- Angular4学习笔记(五)
- Angular4 组件通讯方法大全
- Angular4 树组件的使用
- Angular4 开发环境搭建
- 智力答题查询器,适用于新英雄年代和征途
- IEDA学习之Maven项目导入idea步骤详解
- 正则表达式
- static_cast、dynamic_cast、const_cast和reinterpret_cast总结
- 基于vue-cli的Webpack构建
- Spring cloud 学习(一)
- (五)Angular4 英雄征途HeroConquest-多组件开发
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- ruby on rails 里pluck和select(map、collect)的区别
- 二维码用途分类
- linux系统排错与修复
- java
- hdu 6038 Function 循环节
- sed命令中的正则表达式问号、加号、圆括号等需要转义
- c++ 类模板 用栈实现队列