refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE: Previously, components that would implement lifecycle interfaces would include methods like "onChanges" or "afterViewInit." Given that components were at risk of using such names without realizing that Angular would call the methods at different points of the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods, far reducing the risk of an accidental name collision. To fix, just rename these methods: * onInit * onDestroy * doCheck * onChanges * afterContentInit * afterContentChecked * afterViewInit * afterViewChecked * _Router Hooks_ * onActivate * onReuse * onDeactivate * canReuse * canDeactivate To: * ngOnInit, * ngOnDestroy, * ngDoCheck, * ngOnChanges, * ngAfterContentInit, * ngAfterContentChecked, * ngAfterViewInit, * ngAfterViewChecked * _Router Hooks_ * routerOnActivate * routerOnReuse * routerOnDeactivate * routerCanReuse * routerCanDeactivate The names of lifecycle interfaces and enums have not changed, though interfaces have been updated to reflect the new method names. Closes #5036
This commit is contained in:
@ -473,19 +473,19 @@ export class ChangeDetectorJITGenerator {
|
||||
/** @internal */
|
||||
_genOnCheck(r: ProtoRecord): string {
|
||||
var br = r.bindingRecord;
|
||||
return `if (!throwOnChange) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.doCheck();`;
|
||||
return `if (!throwOnChange) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.ngDoCheck();`;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_genOnInit(r: ProtoRecord): string {
|
||||
var br = r.bindingRecord;
|
||||
return `if (!throwOnChange && ${this._names.getStateName()} === ${this.changeDetectorStateVarName}.NeverChecked) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.onInit();`;
|
||||
return `if (!throwOnChange && ${this._names.getStateName()} === ${this.changeDetectorStateVarName}.NeverChecked) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.ngOnInit();`;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_genOnChange(r: ProtoRecord): string {
|
||||
var br = r.bindingRecord;
|
||||
return `if (!throwOnChange && ${CHANGES_LOCAL}) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.onChanges(${CHANGES_LOCAL});`;
|
||||
return `if (!throwOnChange && ${CHANGES_LOCAL}) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.ngOnChanges(${CHANGES_LOCAL});`;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
|
@ -189,11 +189,11 @@ export class CodegenLogicUtil {
|
||||
var dir = directiveRecords[i];
|
||||
if (dir.callAfterContentInit) {
|
||||
res.push(
|
||||
`if(${this._names.getStateName()} ${eq} ${this._changeDetectorStateName}.NeverChecked) ${this._names.getDirectiveName(dir.directiveIndex)}.afterContentInit();`);
|
||||
`if(${this._names.getStateName()} ${eq} ${this._changeDetectorStateName}.NeverChecked) ${this._names.getDirectiveName(dir.directiveIndex)}.ngAfterContentInit();`);
|
||||
}
|
||||
|
||||
if (dir.callAfterContentChecked) {
|
||||
res.push(`${this._names.getDirectiveName(dir.directiveIndex)}.afterContentChecked();`);
|
||||
res.push(`${this._names.getDirectiveName(dir.directiveIndex)}.ngAfterContentChecked();`);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
@ -207,11 +207,11 @@ export class CodegenLogicUtil {
|
||||
var dir = directiveRecords[i];
|
||||
if (dir.callAfterViewInit) {
|
||||
res.push(
|
||||
`if(${this._names.getStateName()} ${eq} ${this._changeDetectorStateName}.NeverChecked) ${this._names.getDirectiveName(dir.directiveIndex)}.afterViewInit();`);
|
||||
`if(${this._names.getStateName()} ${eq} ${this._changeDetectorStateName}.NeverChecked) ${this._names.getDirectiveName(dir.directiveIndex)}.ngAfterViewInit();`);
|
||||
}
|
||||
|
||||
if (dir.callAfterViewChecked) {
|
||||
res.push(`${this._names.getDirectiveName(dir.directiveIndex)}.afterViewChecked();`);
|
||||
res.push(`${this._names.getDirectiveName(dir.directiveIndex)}.ngAfterViewChecked();`);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
@ -155,12 +155,12 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
|
||||
|
||||
if (proto.isLifeCycleRecord()) {
|
||||
if (proto.name === "DoCheck" && !throwOnChange) {
|
||||
this._getDirectiveFor(directiveRecord.directiveIndex).doCheck();
|
||||
this._getDirectiveFor(directiveRecord.directiveIndex).ngDoCheck();
|
||||
} else if (proto.name === "OnInit" && !throwOnChange &&
|
||||
this.state == ChangeDetectorState.NeverChecked) {
|
||||
this._getDirectiveFor(directiveRecord.directiveIndex).onInit();
|
||||
this._getDirectiveFor(directiveRecord.directiveIndex).ngOnInit();
|
||||
} else if (proto.name === "OnChanges" && isPresent(changes) && !throwOnChange) {
|
||||
this._getDirectiveFor(directiveRecord.directiveIndex).onChanges(changes);
|
||||
this._getDirectiveFor(directiveRecord.directiveIndex).ngOnChanges(changes);
|
||||
}
|
||||
} else if (proto.isSkipRecord()) {
|
||||
protoIdx += this._computeSkipLength(protoIdx, proto, this.values);
|
||||
@ -195,11 +195,11 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
|
||||
for (var i = dirs.length - 1; i >= 0; --i) {
|
||||
var dir = dirs[i];
|
||||
if (dir.callAfterContentInit && this.state == ChangeDetectorState.NeverChecked) {
|
||||
this._getDirectiveFor(dir.directiveIndex).afterContentInit();
|
||||
this._getDirectiveFor(dir.directiveIndex).ngAfterContentInit();
|
||||
}
|
||||
|
||||
if (dir.callAfterContentChecked) {
|
||||
this._getDirectiveFor(dir.directiveIndex).afterContentChecked();
|
||||
this._getDirectiveFor(dir.directiveIndex).ngAfterContentChecked();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -209,10 +209,10 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
|
||||
for (var i = dirs.length - 1; i >= 0; --i) {
|
||||
var dir = dirs[i];
|
||||
if (dir.callAfterViewInit && this.state == ChangeDetectorState.NeverChecked) {
|
||||
this._getDirectiveFor(dir.directiveIndex).afterViewInit();
|
||||
this._getDirectiveFor(dir.directiveIndex).ngAfterViewInit();
|
||||
}
|
||||
if (dir.callAfterViewChecked) {
|
||||
this._getDirectiveFor(dir.directiveIndex).afterViewChecked();
|
||||
this._getDirectiveFor(dir.directiveIndex).ngAfterViewChecked();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user