这是我在尝试使子代调用父代的方法时遇到的错误:
孩子是最喜欢的组件。该方法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!");
}
}
Run Code Online (Sandbox Code Playgroud)
app.component.html
<favorite
[is-favorite] = "post.isFavorite"
(change) = "onFavoriteChange()"
></favorite>
Run Code Online (Sandbox Code Playgroud)
最喜欢的组件
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() {
}
}
Run Code Online (Sandbox Code Playgroud)
在app.component.html中进行更改
(change) = "onFavoriteChange()"
Run Code Online (Sandbox Code Playgroud)
到(首字母大写)
(change) = "OnFavoriteChange()"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3685 次 |
| 最近记录: |