这是我在尝试使子级调用父级方法时遇到的错误:
孩子是最喜欢的组件。该方法onFavoriteChange()
存在于父级中,但未触发。
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Ivans-world';
post = {
title:"Titulo",
isFavorite: true
}
OnFavoriteChange(){
console.log("App Component. Triggered OnChanges(). Yupi!");
}
}
app.component.html
<favorite
[is-favorite] = "post.isFavorite"
(change) = "onFavoriteChange()"
></favorite>
最喜欢的组件
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { empty } from 'rxjs';
@Component({
selector: 'favorite',
templateUrl: './favorite.component.html',
styleUrls: ['./favorite.component.css'],
})
export class FavoriteComponent implements OnInit {
@Input('is-favorite') isFavorite: boolean;
@Output() change = new EventEmitter();
OnClick(){
this.isFavorite = !(this.isFavorite);
this.change.emit();
}
ngOnInit() {
}
}
在app.component.html中进行更改
(change) = "onFavoriteChange()"
到(首字母大写)
(change) = "OnFavoriteChange()"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句