我已经围绕这个问题做了一些研究,现在我不确定这是一个错误还是我的实现有问题。
我有一个使用服务来获取一些数据的元素,然后它将数据传递给一个子元素,但是当我更新数据时,我得到了那个错误。
父元素
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>
当我加载应用程序时,一切都很好,但是如果我单击计数按钮,我就会遇到该错误。