简体   繁体   English

Angular 父组件模板更改后如何从子调用方法

[英]Angular How to call method from child after parent component template changes

I have a parent component, it has a side menu and content.我有一个父组件,它有一个侧面菜单和内容。 Inside the content, a child component is rendered using router-outlet.在内容内部,使用 router-outlet 呈现子组件。

|   menu  |         content               |
|         | |---------------------------| |
|         | |        child              | |
|         | |                           | |
|         | |        xterm              | |
|         | |                           | |
|         | |---------------------------| |

At the push of a button calls method按下按钮调用方法

toggleMenu(){
    isCollapsed = !isCollapsed
    store$.dispatch(sidenavUpdated(data))
}

the menu collapses or expands菜单折叠或展开

<mat-sidenav [fxFlex]="sidenavWidth" ... 
get sidenavWidth(){
     If(isCollapsed){
      return 40px
    }
   return 200px }

I should to wait for the content width to change and then in the child component, run the method to re-render the web shell (xterm.js).我应该等待内容宽度发生变化,然后在子组件中,运行重新渲染 web shell (xterm.js) 的方法。 The child component is subscribed to changes in the store.子组件订阅存储中的更改。

store$.pipe(...).subscribe (()=>{resizeTerm();})

In the debugger, the sequence of code execution is as follows在调试器中,代码执行顺序如下

  1. click -> toggleMenu()单击->切换菜单()
  2. store$.dispatch(...商店$.dispatch(...
  3. in child store$.pipe(...).subscribe (()=>{resizeTerm();}) //get a notification from the store and start redrawing the web shell in child store$.pipe(...).subscribe (()=>{resizeTerm();}) //从 store 获取通知并开始重绘 web shell
  4. [fxFlex]="sidenavWidth" //get width from sidenavWidth() [fxFlex]="sidenavWidth" //从 sidenavWidth() 获取宽度

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? 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?

This problem can have two solutions这个问题可以有两种解决方案

  • calculate the time of collapsing menu as x and use this time as arg of debounceTime operator in subscribing the store将折叠菜单的时间计算为x并将此时间用作debounceTime运算符的 arg 订阅store
store$.pipe(debounceTime(x), ...)
  • use ResizeObserver api for content element and check if the element has the desired size or not使用ResizeObserver api 作为内容元素并检查元素是否具有所需的大小

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

相关问题 如何在Angular 6中从父组件调用子组件的方法 - How to call child components's method from the parent component in Angular 6 Angular 4 - 如何在没有EventEmitter的情况下从子组件调用父方法? - Angular 4 - How to call parent method from child component without EventEmitter? Angular4-从父组件到子模式组件的方法调用 - Angular4 - Call a method from parent component to child modal component 从父组件角度调用子组件方法 - Angular call child component method from parent component 我的子组件如何在Angular 2中的父组件上调用方法? - How can my child component call a method on the parent component in Angular 2? 仅在订阅 httpClient observables 后,如何从父组件方法调用子组件方法? - How to call child component method from parent component method only after subscribing httpClient observables? 如何在子组件的ngOnInit之后调用父组件中的方法 - how to call method in parent component after ngOnInit of child component 如何从Angular2父组件中的组件列表中调用子组件中的方法? - How to call a method in child component from a list of components in parent component in Angular2? 如何在promise中从角度4的子组件中调用父组件中的方法? - How to call a method in parent component from child component in angular 4 within a promise? 从父类调用子组件方法 - Angular - Call child component method from parent class - Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM