天天看點

關于Angular Component changeDetection政策設定成OnPush的一個單元測試局限性

關于Angular Component changeDetection政策設定成OnPush的一個單元測試局限性
關于Angular Component changeDetection政策設定成OnPush的一個單元測試局限性
關于Angular Component changeDetection政策設定成OnPush的一個單元測試局限性

OnPush: 0

Use the CheckOnce strategy, meaning that automatic change detection is deactivated until reactivated by setting the strategy to Default (CheckAlways). Change detection can still be explicitly invoked. This strategy applies to all child directives and cannot be overridden.

注意:仍然可以顯式觸發。

Default: 1

Use the default CheckAlways strategy, in which change detection is automatic until explicitly deactivated.

我們甚至可以更改某個Component的change detection頻率。

Detach change detector to limit how often check occurs

The following example defines a component with a large list of read-only data that is expected to change constantly, many times per second. To improve performance, we want to check and update the list less often than the changes actually occur. To do that, we detach the component’s change detector and perform an explicit local check every five seconds.

class DataListProvider {

 // in a real application the returned data will be different every time

 get data() {

   return [1, 2, 3, 4, 5];

 }

}

@Component({

 selector: 'giant-list',

 template: `

  • Data {{d}}
  •    `,

    })

    class GiantList {

     constructor(private ref: ChangeDetectorRef, public dataProvider: DataListProvider) {

       ref.detach();

       setInterval(() => {

         this.ref.detectChanges();

       }, 5000);

     selector: 'app',

     providers: [DataListProvider],

       `,

    class App {

    Angular應用 changeDetection模式的設定架構代碼

    根據關鍵字ChangeDetectionStrategy搜尋即可。

    const createInject = makeMetadataFactory('Inject', (token) => ({ token }));

    const createInjectionToken = makeMetadataFactory('InjectionToken', (desc) => ({ _desc: desc, ɵprov: undefined }));

    const createAttribute = makeMetadataFactory('Attribute', (attributeName) => ({ attributeName }));

    const createContentChildren = makeMetadataFactory('ContentChildren', (selector, data = {}) => (Object.assign({ selector, first: false, isViewQuery: false, descendants: false }, data)));

    const createContentChild = makeMetadataFactory('ContentChild', (selector, data = {}) => (Object.assign({ selector, first: true, isViewQuery: false, descendants: true }, data)));

    const createViewChildren = makeMetadataFactory('ViewChildren', (selector, data = {}) => (Object.assign({ selector, first: false, isViewQuery: true, descendants: true }, data)));

    const createViewChild = makeMetadataFactory('ViewChild', (selector, data) => (Object.assign({ selector, first: true, isViewQuery: true, descendants: true }, data)));

    const createDirective = makeMetadataFactory('Directive', (dir = {}) => dir);

    var ViewEncapsulation;

    (function (ViewEncapsulation) {

       ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";

       ViewEncapsulation[ViewEncapsulation["Native"] = 1] = "Native";

       ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";

       ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";

    })(ViewEncapsulation || (ViewEncapsulation = {}));

    var ChangeDetectionStrategy;

    (function (ChangeDetectionStrategy) {

       ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";

       ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";

    })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));

    const createComponent = makeMetadataFactory('Component', (c = {}) => (Object.assign({ changeDetection: ChangeDetectionStrategy.Default }, c)));

    const createPipe = makeMetadataFactory('Pipe', (p) => (Object.assign({ pure: true }, p)));

    const createInput = makeMetadataFactory('Input', (bindingPropertyName) => ({ bindingPropertyName }));

    const createOutput = makeMetadataFactory('Output', (bindingPropertyName) => ({ bindingPropertyName }));

    const createHostBinding = makeMetadataFactory('HostBinding', (hostPropertyName) => ({ hostPropertyName }));

    const createHostListener = makeMetadataFactory('HostListener', (eventName, args) => ({ eventName, args }));

    const createNgModule = makeMetadataFactory('NgModule', (ngModule) => ngModule);

    const createInjectable = makeMetadataFactory('Injectable', (injectable = {}) => injectable);

    //告訴Angular,你正在使用自定義元素

    const CUSTOM_ELEMENTS_SCHEMA = {

       name: 'custom-elements'

    };

    ```![在這裡插入圖檔描述](https://img-blog.csdnimg.cn/20210213110729716.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2kwNDI0MTY=,size_16,color_FFFFFF,t_70)

    繼續閱讀