简体   繁体   English

当子组件位于路由器出口Angular中时,从子组件调用父组件函数

[英]Call parent component function from child when child components are in router outlet Angular

I need to update some data in parent component by calling a function in it after an event has occurred in the child component. 我需要通过在子组件中发生事件后在其中调用函数来更新父组件中的某些数据。 Here my child components are in router outlet of parent 这里我的孩子组件在父母的路由器出口

parentComponent.html:- parentComponent.html: -

<div class="row">
     Parent component
</div>
<router-outlet></router-outlet>

Parentcomponent.ts:- Parentcomponent.ts: -

fetchRequestsStatus(role, user) {
if (this.userRole) {
  this.dashboardService.getStatusCount(role, user).subscribe(
    res => {
      console.log(res)
    },
    err => {
      console.log(err)
    })
}

} }

Now on a button click in the child, I need to call function fetchRequestsStatus(role, user) by passing a parameter from the child. 现在,单击子项中的按钮,我需要通过传递子项中的参数来调用函数fetchRequestsStatus(role, user) I have seen @input @ output decorators for sharing data but here my child component is inside the router. 我已经看到@input @输出装饰器用于共享数据,但是这里我的子组件位于路由器内部。

How can I proceed with this? 我该如何进行呢?

If you're thinking something like 如果您在想类似

<router-outlet (onButtonClick)="fetchRequestsStatus($events)"></router-outlet>

it's invalid as the child component you're thinking is a child component is actually a sibling component at runtime. 这是无效的,因为您正在考虑的子组件实际上是运行时的同级组件。

I am guessing this is your app component. 我猜这是您的应用程序组件。 So, you can have a subject in the app.service which can be an observer for the child and an observable for the parent. 因此,您可以在app.service有一个主题,该app.service可以是孩子的观察者,而父对象可以观察。

The cleanest solution would be to create a shared service between the parent and child component, service that holds an Subject used by parent and child component. 最干净的解决方案是在父子组件之间创建共享服务,该服务保存父子组件使用的主题。

Refer to this answer for code sample: https://stackoverflow.com/a/41989983/5620535 请参考以下答案以获取代码示例: https : //stackoverflow.com/a/41989983/5620535

Make sure you use the new providedIn: 'root' angular feature to make sure the service is only instantiated once. 确保使用新的providedIn: 'root'角度功能,以确保仅实例化该服务一次。 (singleton) (singleton)的

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

相关问题 如何从父组件调用路由器出口子组件方法 - How to call router outlet child component method from parent comonent 来自儿童路线的Angular 4父路由器插座 - Angular 4 Parent Router Outlet from Child Route 将子组件传递给父组件(路由器插座中的子渲染)? 在 Angular 中? - Pass child to parent component (child render in router-outlet) ? in Angular? Angular 5从子插座在父路由器出口上触发导航 - Angular 5 trigger navigation on parent router-outlet from child outlet Angular2中的子级属性更改时更新父级属性(使用路由出口加载的子级组件) - Update Parent Property When Child Property Changes in Angular2 ( Child component loaded using router-outlet) Angular 2:让子路由组件替换路由器插座中的Parent - Angular 2: Have Child Route Component Replace Parent in router-outlet 角路由器如何从父组件调用子组件 - angular router how to call child component from the parent component 从父级调用子级方法(并传递数据),子级将在 angular 中动态加载组件(路由器出口或延迟加载子级) - Invoke Child method(and pass data) from parent, Child will be dynamically loaded component(router-outlet or lazy loaded child) in angular Angular子路由器出口中组件的条件渲染 - Conditional rendering of components in Angular child router outlet 从子组件路由到父路由器出口 - Routing to parent's router-outlet from a child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM