![](/img/trans.png)
[英]How to call child components's method from the parent component in Angular 6
[英]Angular How to call method from child after parent component template changes
我有一個父組件,它有一個側面菜單和內容。 在內容內部,使用 router-outlet 呈現子組件。
| menu | content |
| | |---------------------------| |
| | | child | |
| | | | |
| | | xterm | |
| | | | |
| | |---------------------------| |
按下按鈕調用方法
toggleMenu(){
isCollapsed = !isCollapsed
store$.dispatch(sidenavUpdated(data))
}
菜單折疊或展開
<mat-sidenav [fxFlex]="sidenavWidth" ...
get sidenavWidth(){
If(isCollapsed){
return 40px
}
return 200px }
我應該等待內容寬度發生變化,然后在子組件中,運行重新渲染 web shell (xterm.js) 的方法。 子組件訂閱存儲中的更改。
store$.pipe(...).subscribe (()=>{resizeTerm();})
在調試器中,代碼執行順序如下
My web shell redraws before the parent template gets the new width for ".content" How can I wait for a change in the parent's html and then re-render mine web shell?
這個問題可以有兩種解決方案
x
並將此時間用作debounceTime
運算符的 arg 訂閱store
store$.pipe(debounceTime(x), ...)
ResizeObserver
api 作為內容元素並檢查元素是否具有所需的大小
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.