angular – 如何将子组件传递给父级兄弟组件?
发布时间:2020-12-17 18:10:50 所属栏目:安全 来源:网络整理
导读:我想在list.component.ts上的用户mouSEOver / mouseleave时显示来自bucket-modal.component.ts的弹出窗口. 如何在list.component.ts和bucket-modal.component.ts之间进行通信?我的代码在这里. list.component.ts @Component({ selector: 'list',templateUrl
我想在list.component.ts上的用户mouSEOver / mouseleave时显示来自bucket-modal.component.ts的弹出窗口.
如何在list.component.ts和bucket-modal.component.ts之间进行通信?我的代码在这里. list.component.ts @Component({ selector: 'list',templateUrl: 'list.component.html',styleUrls: ['list.component.css'],}) export class ListComponent implements OnInit { @Input() state: boolean; @Output() toggle = new EventEmitter(); onHover() { this.state = true; this.toggle.emit(this.state); console.log("state is ----------" + this.state); } onHoverOut() { this.state = false; this.toggle.emit(this.state); console.log("state is------ " + this.state); } } list.component.html <a (mouSEOver)="onHover()" (mouseleave)="onHoverOut()">random Link list</a> listdetails.component.ts @Component({ selector: 'app-list-detail',templateUrl: 'app-list.component.html',styleUrls: ['app-list.component.css'],}) export class ListDetailComponent implements OnInit { } listdetails.component.html <list [elementslist]="listdetails" listingtype="3"></list> <list [elementslist]="listdetails" listingtype="3"></list> <list [elementslist]="listdetails" listingtype="3"></list> <bucket-modal [(showMeaddBucket)]="show2ClickedBucket" [state]="PopUpshow" (toggle)="PopUpshow=$event"></bucket-modal> 斗modal.component.ts @Component({ selector: 'bucket-modal',templateUrl: 'bucket-modal.component.html',styleUrls: ['bucket-modal.component.css'],}) export class BucketModalComponent implements OnInit { @Input() state: boolean; @Output() toggle = new EventEmitter(); onHover() { this.state = true; this.toggle.emit(this.state); console.log("state is " + this.state); } onHoverOut() { this.state = false; this.toggle.emit(this.state); console.log("state is " + this.state); } } 解决方法
我认为最简单的方法是在BucketModalComponent中创建一个公共方法,它将显示弹出对话框.就像是
export class BucketModalComponent implements OnInit { showDialog(): void { // Open the popup dialog } } 然后你可以在listdetails.component.html中调用它: <list ... (toggle)="modal.showDialog()"></list> <bucket-modal #modal ... ></bucket-modal> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |