亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

角度渲染組件首先,然后在 ng-If 中刪除另一個組件

角度渲染組件首先,然后在 ng-If 中刪除另一個組件

侃侃無極 2022-09-29 17:54:59
我使用ngif指令一次顯示一個組件。<app-root>   <first-Comp *ngIf="showFirst"></first-Comp>   <second-Comp *ngIf="!showFirst"></second-Comp></app-root>要點是顯示第一變量使用 true 進行初始化。第一合成包含高度為100px的元素;第二比較有動態元素在第二個組件內部,我使用內部計算高度document.body.scrollHeightngOnInit問題是當 變為角時,首先呈現第二個合成,然后刪除 .結果,我得到的高度是100+而不是0。但是我需要身體的高度,只在組件渲染。showFristfalsefirst-compsecond-comp我錯過了另一件重要的事情,因為我認為這可能不會妨礙。即第一個和第二個組件都與角度自動變化檢測分離以提高性能。我有一個這樣的基本組件export class BaseComponent {private subscriptions: Subscription[] = [];  constructor(private childViewRef: ChangeDetectorRef) {      this.childViewRef.detach();  }  public updateUI(): void {    try {        this.childViewRef.reattach();        this.childViewRef.detectChanges();        this.childViewRef.detach();    } catch (ex) {        // ignored    }  }  protected addSubscriptions(subs: Subscription) {    this.subscriptions.push(subs);  }  protected unSubscribeSubscriptions() {    this.subscriptions.forEach(item => item.unsubscribe());    this.subscriptions = [];  }}除應用程序組件外,所有組件都繼承此基本組件,因此第二組件的代碼如下所示。@Component({  selector: 'second-comp',  templateUrl: './SecondComponent.component.html',  styleUrls: ['./SecondComponent.component.css'],  changeDetection: ChangeDetectionStrategy.OnPush})export class SecondComponent extends BaseComponent implements OnInit, AfterViewInit{   constructor(private ref:ChangeDetectorRef){     super(ref);     }   ngAfterViewInit(): void {     this.updateUi();     this.publishHeight()   }   ngOnInit() {    this.updateUi();    this.publishHeight()   }}有什么不對嗎,我得到了這種意想不到的行為。
查看完整描述

3 回答

?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

計算在 ngOn 初始化內的第二次合成中設置超時() 中的高度

 setTimeout(() => { 
          //calculateHeight()  
        }, 200);



查看完整回答
反對 回復 2022-09-29
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

感覺你做錯了。您可以在第二復合構造函數中注入@Self,它將為您提供自身的元素Ref(第二復合)。


constructor( @Self() private element: ElementRef ) {}

它可能不起作用,但不會受到第一次競爭的影響


ngOnInit() {

    this.element.nativeElement.offsetHeight //the height for whatever you need it for

}


查看完整回答
反對 回復 2022-09-29
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您應該計算組件視圖完全呈現時的高度。這意味著計算 ng 內部的高度后查看初始化() 鉤子。請參閱 https://angular.io/api/core/AfterViewInit


查看完整回答
反對 回復 2022-09-29
  • 3 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號