【发布时间】:2020-06-10 02:10:56
【问题描述】:
各位,谢谢你们的帮助。在获得大量建议并混合并匹配所有这些建议的代码后,我能够解决早期的问题。但是,在控制台中使用 @ViewChild 装饰器后出现以下错误。
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'count1'. Current value: 'count2'.
我的父 ts 文件代码如下
import { Component, OnInit, Input, ViewEncapsulation, ViewChild, AfterViewInit} from '@angular/core';
import { NagivationComponent } from '../nagivation/nagivation.component';
import { CensusComponent } from '../your-data/census/census.component';
import { ApplicationComponent } from '../your-data/application/application.component';
@Component({
selector: 'app-your-data',
templateUrl: './your-data.component.html',
styleUrls: ['./your-data.component.css'],
encapsulation: ViewEncapsulation.None
})
export class YourDataComponent implements AfterViewInit{
@Input() step;
count:string = 'count1';
constructor() {
}
@ViewChild(CensusComponent, {static: false}) census: CensusComponent;
ngAfterViewInit() {
this.count = this.census.getSection();
}
}
your-data.component.html 代码如下
<div [ngSwitch]="count">
<app-census *ngSwitchCase="'count1'"></app-census>
<app-application *ngSwitchCase="'count2'"></app-application>
</div>
【问题讨论】:
-
你试过 *ngIf 吗?
-
我需要在哪里使用 *ngIf?
-
这能回答你的问题吗? Angular 2 Show and Hide an element
标签: angular angular-services angular-components