gpt4 book ai didi

Angular 5 EventEmitter 运行不正常

转载 作者:太空狗 更新时间:2023-10-29 18:19:31 26 4
gpt4 key购买 nike

我试图在子组件上调用父组件函数,并像这样实现。

project-form.component.ts

@Component({
selector: 'app-project-form',
templateUrl: './project-form.component.html',
styleUrls: ['./project-form.component.css'],
encapsulation: ViewEncapsulation.None
})
export class ProjectFormComponent implements OnInit {

@Input() project: Project;
@Output() createProject = new EventEmitter<string>();

newProject() {
console.log("submit");
this.createProject.emit('submit');
}
}

project-form.component.html

<button (click)="newProject()" class = "btn-success" >New</button>

project-view.component.ts

export class ProjectViewComponent implements OnInit {

projects: Project[];
projectform: Project;

createProject(event){
console.log("create Project on parent component");
}
}

project-view.component.html

<app-project-form [project]="projectform" (createProject)="createProject($event)"></app-project-form>

在这里,当我点击子组件的 New 按钮时,我只能在控制台上看到 "submit" 而看不到 "create Project on parent component".

表示事件未发出或父级未收到事件。基本上,我错过了一些东西。
请为我分享任何类型的点击。

最佳答案

你错过了一件事@artgb,createProject(event:string){}你试试这个。

关于Angular 5 EventEmitter 运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47200666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com