Angular Component的默认changeDetection策略

260 阅读1分钟

默认策略为:ChangeDetectionStrategy.Default:

实现代码:

/**
 * Type of the Directive metadata.
 *
 * @publicApi
 */
const Directive = makeDecorator('Directive', ɵ0$e, undefined, undefined, ɵ1$3);
const ɵ2$1 = (c = {}) => (Object.assign({ changeDetection: ChangeDetectionStrategy.Default }, c)), ɵ3$1 = (type, meta) => SWITCH_COMPILE_COMPONENT(type, meta);
/**
 * Component decorator and metadata.
 *
 * @Annotation
 * @publicApi
 */
const Component = makeDecorator('Component', ɵ2$1, Directive, undefined, ɵ3$1);
const ɵ4 = (p) => (Object.assign({ pure: true }, p)), ɵ5 = (type, meta) => SWITCH_COMPILE_PIPE(type, meta);
/**
 * @Annotation
 * @publicApi
 */
const Pipe = makeDecorator('Pipe', ɵ4, undefined, undefined, ɵ5);
const ɵ6 = (bindingPropertyName) => ({ bindingPropertyName });
/**
 * @Annotation
 * @publicApi
 */
const Input = makePropDecorator('Input', ɵ6);
const ɵ7 = (bindingPropertyName) => ({ bindingPropertyName });
/**
 * @Annotation
 * @publicApi
 */
const Output = makePropDecorator('Output', ɵ7);
const ɵ8 = (hostPropertyName) => ({ hostPropertyName });
/**
 * @Annotation
 * @publicApi
 */
const HostBinding = makePropDecorator('HostBinding', ɵ8);
const ɵ9 = (eventName, args) => ({ eventName, args });

在SAP Spartacus大多数Component里,我们都用@Component将changeDetection显式指定成ChangeDetectionStrategy.OnPush:


OnPush是另一种策略,用于提高Angular 应用的性能。

OnPush工作模式下,只有input ref change和output’s call会触发change detection机制。

TestBed.overrideComponent提供了一种能够覆盖标准ChangeDetectionStrategy的机制。

更多Jerry的原创文章,尽在:“汪子熙”: