Angular 7 - 在向兄弟组件发送Output()后调用兄弟组件内的函数
创始人
2024-10-16 16:31:39
0

在Angular 7中,你可以使用@ViewChild装饰器来获取兄弟组件的实例,并在父组件中调用其方法。以下是一个示例解决方法:

首先,在兄弟组件中定义一个公共方法,可以供其他组件调用:

// 兄弟组件
export class BrotherComponent {
  // 定义一个公共方法
  public doSomething(): void {
    console.log('兄弟组件的方法被调用');
  }
}

然后,在父组件中使用@ViewChild装饰器来获取兄弟组件的实例,并在需要的时候调用它的方法:

// 父组件
import { Component, ViewChild } from '@angular/core';
import { BrotherComponent } from '兄弟组件的路径';

@Component({
  selector: 'app-parent',
  template: `
    
  `
})
export class ParentComponent {
  @ViewChild(BrotherComponent) brotherComponent: BrotherComponent;

  handleOutputEvent(event: any): void {
    // 在需要的时候调用兄弟组件的方法
    this.brotherComponent.doSomething();
  }
}

在上述示例中,@ViewChild(BrotherComponent) brotherComponent: BrotherComponent;装饰器将兄弟组件的实例赋值给brotherComponent变量。然后,在父组件的handleOutputEvent方法中,你可以通过this.brotherComponent来访问兄弟组件的公共方法doSomething()

这样,当兄弟组件触发outputEvent事件时,父组件会调用兄弟组件的doSomething()方法。

相关内容

热门资讯

记者揭秘!德普之星辅助工具如何... 记者揭秘!德普之星辅助工具如何设置,hhpoker德州真的假的,揭秘教程(有挂软件)1、很好的工具软...
盘点一款!pokemmo手机脚... 盘点一款!pokemmo手机脚本辅助器,wepoker有辅助功能吗,解密教程(有挂辅助);建议优先通...
解密关于!wepoker辅助是... 1、解密关于!wepoker辅助是真的吗,wpk辅助器,AI教程(有挂技巧)。2、wepoker辅助...
推荐攻略!we-poker辅助... 推荐攻略!we-poker辅助,hhpoker有后台操作吗,切实教程(有挂辅助)关于hhpoker有...
每日必备!pokemmo手机版... 每日必备!pokemmo手机版透视脚本,wpk辅助器,透明教程(有挂神器);建议优先通过pokemm...
一分钟揭秘!hhpoker是正... 《一分钟揭秘!hhpoker是正规的吗,wepoker怎么挂底牌,玩家教你(有挂透明)》 hhpok...
我来教教大家!wepoker看... 我来教教大家!wepoker看底牌,约局吧德州可以透视吗,揭秘攻略(有挂透视)1、不需要AI权限,帮...
记者揭秘!epoker透视底牌... 记者揭秘!epoker透视底牌,大菠萝辅助器,2025教程(有挂方法);玩家必备必赢加哟《13670...
教程攻略!pokemmo辅助器... 1、教程攻略!pokemmo辅助器手机版下载,智星德州插件2024最新版,玩家教程(有挂技巧)2、进...
玩家交流!hhpoker必备开... 玩家交流!hhpoker必备开挂,wepoker有没有辅助,微扑克教程(有挂攻略);1分钟了解详细教...