Angular 事件发射器在子组件中不工作
在我的angular应用程序中,若我通过url路由,事件发射器发出的事件不会在父组件中触发 例: 如果我在浏览器中键入完整的url。http://localhost:4200/child 我的子组件正在加载。但close事件发射器发出的事件并没有在父组件中触发。下面是我的代码 文件:Angular 事件发射器在子组件中不工作,angular,ionic-framework,Angular,Ionic Framework,在我的angular应用程序中,若我通过url路由,事件发射器发出的事件不会在父组件中触发 例: 如果我在浏览器中键入完整的url。http://localhost:4200/child 我的子组件正在加载。但close事件发射器发出的事件并没有在父组件中触发。下面是我的代码 文件:app.routing.module.ts const routes: Routes = [ {path : 'child', component : ChildComponent},
app.routing.module.ts
const routes: Routes = [
{path : 'child', component : ChildComponent},
];
cancel() {
this.show = false;
}
@Output() close = new EventEmitter<void>();
cancel() {
this.close.emit();
}
文件:app.component.html
<div>
<app-bibdpayment *ngIf="show == true" (close)="cancel()"></app-bibdpayment>
<router-outlet></router-outlet>
<button class="btn btn-primary" (click)="cancel()">Cancel</button>
文件:childcomponent.html
<div>
<app-bibdpayment *ngIf="show == true" (close)="cancel()"></app-bibdpayment>
<router-outlet></router-outlet>
<button class="btn btn-primary" (click)="cancel()">Cancel</button>
事件发射器不工作的原因。当我们直接调用子组件url时
如何解决这个问题。任何帮助都将不胜感激 在模板中为show指定true,因此show的值始终包含相同的值。因此,您必须从component.ts文件管理show的值。为了让您更好地理解,我在下面给出了代码详细信息: ChildComponent.ts 然后声明您的函数并按照ParantComponent.ts文件中的步骤进行操作:
show = true;
close()
{
this.show = false;
}
希望它能起作用。谢谢((close)=“取消($event)
和此.close.emit(1)
尝试使用此更新并在控制台中检查是否接收到事件?