Angular2 - Starter - Component and Component Lifecircle Hooks
我们通过一个NgModule来启动一个ng app,NgModule通过bootstrap配置来指定应用的入口组件。
@NgModule({
bootstrap: [ AppComponent ],
....
}
在 declarations 可以配置sub_component
declarations: [
FriendsComponent,
ChatsComponent
]
如下构造一个TestComponent:
import {
Component, OnInit, Input, OnChanges, SimpleChanges, AfterContentInit, AfterContentChecked, AfterViewChecked,
AfterViewInit,DoCheck,OnDestroy, Output, EventEmitter, HostBinding, HostListener
} from '@angular/core';
import {AppComponent} from '../app.component';
@Component({
/*
定义css选择器,
使用:
<app-test [app]="config"></app-test>
[app]="config" 表示父组件向TestComponent输入的信息
*/
selector: 'app-test',
/*
组件的模板文件,如果模板文件代码不多,可以使用:
template:'<span></span>'
*/
templateUrl: './test.component.html',
/*
组件的样式,同样可以类似使用:
styles:[
{
color:'red'
}
]
*/
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit,OnChanges,AfterContentInit,AfterContentChecked,AfterViewChecked,AfterViewInit,DoCheck,OnDestroy {
@Input()
app:Object;
@Input('config') set updateParent(app:Object){
this.parent = app;
}
public parent = {};
public title = 'hello';
private name = 'test ng component';
init(){
//this.title = 'hello ng2.'
console.log('inited..');
this.name = this.name + 'in init.';
}
alertName(){
alert(this.name);
}
constructor() {
//构造函数一般用于接收组件初始化时的一些提供商,比如:Http模块,自定义service等;
//不适合写太多的逻辑代码;
/*constructer()constructor(
public appState: AppState,public router:Router,http:Http,public envSvc:EnvSvc) {
this.envSvc = envSvc;
}
*/
this.title = 'test comp1';
this.name = this.name + 'in constructor.';
}
ngOnChanges(changes: SimpleChanges): void {
//在组件数据绑定输入的值发生变化时执行,在本例中,app属性的输入发生变化时,即执行ngOnChanges
//该方法的应用范围是:指令和组件
console.log(changes);
console.log('ngOnChanges ..');
}
ngOnInit() {
//在第一次的ngOnChanges之后执行,此时即开始初始化组件/指令
//该方法在一个 组件/指令 实例的生命周期中只执行一次
//该方法的应用范围是:指令和组件
this.init();
}
ngDoCheck(): void{
//input的属性每次变化都会触发angular的变化检测
// 在每次angular变化检测时执行ngDoCheck
//该方法的应用范围是:指令和组件
console.log('angular is checking changes ');
}
ngAfterContentInit(): void {
//在组件第一次接收数据输入之后,会将数据映射到组件模板
//这个映射完成后执行ngAfterContentInit
//该方法在一个 组件/指令 实例的生命周期中只执行一次
//该方法的应用范围是:指令
console.log('content has been inited...');
}
ngAfterContentChecked(): void {
//在组件检查到数据变化之后,会将新的数据映射到组件模板
//这个映射完成后执行ngAfterContentChecked
//该方法的应用范围是:指令
/*
1.新的数据映射到模板之后,才开始创建视图,即ngAfterContentInit和ngAfterContentChecked在ngAfterViewInit之前执行,
2.子组件的视图初始化或视图检查完成后才会触发当前组件的ngAfterViewInit或ngAfterViewChecked
所以AppComponent的ngAfterViewChecked总是在所有字组件的视图都渲染完毕之后执行
3.在初始化完成之后的每一次数据变化是,总是应用根组件的ngAfterContentChecked先执行,
然后每个子组件/指令内 ngAfterContentChecked()->ngAfterViewChecked()依次执行,
对整个应用而言,最后执行的方法是根组件的ngAfterViewChecked(),
4.一个子组件的数据变化最先触发根组件的ngAfterContentChecked,
然后是中间组件(根组件的子组件,变化组件的父级组件)的ngAfterContentChecked,
之后是变化组件的ngAfterContentChecked()->ngAfterViewChecked()
再执行父组件的ngAfterViewChecked(),
最后才执行根组件的ngAfterViewChecked()
*/
console.log('content has been checked...');
}
ngAfterViewInit(): void {
//在初始化组件视图和子组件视图之后执行
//AfterViewInit就是指:页面视图初始化之后
//该方法在一个 组件/指令 实例的生命周期中执行一次
//该方法的应用范围是:指令
console.log('view has been inited...');
}
ngAfterViewChecked(): void {
//在组件或子组件视图检查之后执行
//在输入数据变化时,ng根据新的数据对页面进行检查和重新渲染
//AfterViewChecked就是:视图检查之后
//该方法的应用范围是:指令
console.log('view has been checked...');
}
ngOnDestroy(): void{
//在组件销毁时执行
//一些组件如果在*ngIf指令之内,条件为false时,该组件会被销毁,此时会执行此方法
//该方法在一个 组件/指令 实例的生命周期中执行一次
//该方法的应用范围是:指令和组件
}
//将某个元素属性绑定到某个指令或者组件的属性
@HostBinding('style.disable') 'disabled';
//将某个事件绑定到组件
@HostListener('click',['$event']) function(){alert('test component @HostListener')
};
}
Angular2 - Starter - Component and Component Lifecircle Hooks的更多相关文章
- angular2 学习笔记 ( Dynamic Component 动态组件)
更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...
- angular2使用ng g component navbar创建组件报错
Error: ELOOP: too many symbolic links encountered, stat 'C:\Users\inn\angulardemo\node_modules\@angu ...
- Angular2 - Starter - Routes, Route Resolver
在基于Angualr的SPA中,路由是一个很重要的部分,它帮助我们更方便的实现页面上区域的内容的动态加载,不同tab的切换,同时及时更新浏览器地址栏的URN,使浏览器回退和前进能导航到历史访问的页面. ...
- Salesforce LWC学习(四) 父子component交互 / component声明周期管理 / 事件处理
我们在上篇介绍了 @track / @api的区别.在父子 component中,针对api类型的变量,如果声明以后就只允许在parent修改,son component修改便会导致报错. sonIt ...
- Angular2 - Starter - Pipes, Custom Pipes
在Angular2 模板中,我们在显示数据时,可以使用通道将数据转换成相应的格式的值的形式来显示,而且这不改变源数据.比如,我们可以使用date通道来转换时间显示的格式: {{date | date: ...
- [转]angular2: including thirdparty js scripts in component
本文转自:https://stackoverflow.com/questions/35570746/angular2-including-thirdparty-js-scripts-in-compon ...
- [React] Refactor a Class Component with React hooks to a Function
We have a render prop based class component that allows us to make a GraphQL request with a given qu ...
- [Angular2 Form] Create custom form component using Control Value Accessor
//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...
- [Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose
Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a cl ...
随机推荐
- 发现一个Doxygen风格的QT帮助
http://cep.xray.aps.anl.gov/software/qt4-x11-4.8.6-browser/classes.html http://cep.xray.aps.anl.gov/ ...
- 解决linux下导入数据库乱码问题
引言:在windows下的mysql数据库导出SQL文件,在Linux下导入后显示为乱码. 1.启动Mysql服务及创建数据库(下面uushop为我将创建的数据库名) service mysqld s ...
- 子查询解嵌套not in 无法展开改写
SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...
- django 常用命令
django 常用命令,备忘: django-admin.py startproject test 创建一个项目名叫test的项目 python manage.py startapp app 创建一个 ...
- ETL构建数据仓库五步法
原文:http://huangy82.blog.163.com/blog/static/49069827200923034638409/ ETL构建企业级数据仓库五步法 在数据仓库构建中,ETL贯穿于 ...
- t-sql 中between and 的一种写法
t-sql 中between and 的一种写法: where GETDATE() BETWEEN BeginDateTime AND EndDateTime; BeginDateTime,EndDa ...
- Naive and Silly Muggles
Problem Description Three wizards are doing a experiment. To avoid from bothering, a special magic i ...
- leecode 回文字符串加强版
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- (转载)TRS内容管理平台用户注册逻辑漏洞
首先 site:gov.cn inurl:WCM TRS 的内容管理系统是国内政府网站使用最多的系统之一 如上面所说:外交部 http://wcm.fmprc.gov.cn/wcm/ 网址加上:wcm ...
- GCC内联汇编入门
原文为GCC-Inline-Assembly-HOWTO,在google上可以找到原文,欢迎指出翻译错误. 中文版说明 由于译者水平有限,故译文出错之处,还请见谅.C语言的关键字不译,一些单词或词组( ...