繁体   English   中英

嵌套的Angular元素抛出ExpressionChangedAfterItHaHasBeenCheckedError

[英]Nested Angular elements throwing ExpressionChangedAfterItHasBeenCheckedError

我已经对此问题进行了一些研究,但现在不确定它是错误还是我的实现出现问题。

我有一个使用服务来获取一些数据的元素,然后将数据传递给子元素,但是当我更新数据时,我遇到了这个错误。

父元素

import { Component, OnInit, ViewEncapsulation} from '@angular/core';
import { TestService } from '../services/test.service';

@Component({
    selector: 'dm-element-with-service-test',
    templateUrl: './element-with-service-test.component.html',
    encapsulation: ViewEncapsulation.Emulated
})
export class ElementWithServiceTestComponent implements OnInit {
currentCount: number;

constructor(private testService: TestService) { }

    ngOnInit() {
        this.testService.count.subscribe(count => this.currentCount = count); 
    }

    count() {
        this.testService.addCount();
    }

}

父元素视图

<div>
    <button (click)="count()">count</button>
    <element-test [myNumber]="currentCount"></element-test>
</div>

子元素

import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';

@Component({
    selector: 'dm-element-test',
    templateUrl: './element-test.component.html',
    encapsulation: ViewEncapsulation.Emulated
})
export class ElementTestComponent implements OnInit {
    @Input() myNumber: number;
    constructor() { }

    ngOnInit() {
    // do stuff...
    }
}

子元素视图

<p>
    current number in this component: {{myNumber}}
</p>

应用模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, Injector, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { AppComponent } from './app.component';
import { ElementTestComponent } from './element-test/element-test.component';
import { createCustomElement } from '@angular/elements';
import { ElementWithServiceTestComponent } from './element-with-service-test/element-with-service-test.component';


@NgModule({
    declarations: [
        AppComponent,
        ElementTestComponent,
        ElementWithServiceTestComponent
    ],
    imports: [
        BrowserModule
    ],
    providers: [],
    schemas: [CUSTOM_ELEMENTS_SCHEMA ],
    entryComponents: [ElementTestComponent, ElementWithServiceTestComponent]
})
export class AppModule {
    constructor(private injector: Injector) {
        const el = createCustomElement(ElementTestComponent, { injector });
        const el2 = createCustomElement(ElementWithServiceTestComponent, { injector });
        customElements.define('element-test', el);
        customElements.define('element-with-service-test', el2);
    }
    ngDoBootstrap() { }
}

指数

<div class="tests">
     <element-with-service-test></element-with-service-test>
</div>

加载应用程序时,一切正常,但是如果单击“计数”按钮,则会出现该错误。

当您更改子组件中从父代传递的数据时,总是会发生该错误。 你应该

1)不这样做

2)修改数据时,请使用changeDetectorRef.detectChanges()

https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM