如何使用EventEmitter将功能传递给子组件?

苏迪普·米什拉

我有一个父子组件设置,在子元素中的(轻击)事件上,我需要调用父方法。我正在尝试eventEmmiter,但遇到此错误:

错误:com.tns.NativeScriptException:无法找到模块:“事件”,相对于:app / tns_modules /

程式码片段

Parent.component.html

<app-header (thisEvent)="navigate($event)"></app-header>

Parent.component.ts

import { Component } from "@angular/core";
@Component({
  moduleId: module.id,
  selector: "app-main",
  templateUrl: "./parent.component.html",
  styleUrls: ["./parent.component.css"]
})
export class ParentComponent {
naviagte(args){
this.router.navigate(["/"]);
}
}

Child.component.html:

<Button text="Tap" class="btn btn-primary" (tap)="onTap($event)"></label>

Child.component.ts:

import { Component, Input, Output } from "@angular/core";
import { EventEmitter } from "events";
@Component({
  selector: "app-header",
  moduleId: module.id,
  templateUrl: "./child.component.html",
  styleUrls: ["./child.component.css"]
})
export class ChildComponent {
  @Output()
  thisEvent= new EventEmitter();
  navigate(args) {
    this.thisEvent.emit(null);
  }
}

应导入EventEmitter来自@angular/core不从events

import { Component, Input, Output, EventEmitter } from "@angular/core";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章