【问题标题】:Class incorrectly implements interface 'OnChanges'. Property 'ngOnChanges' is missing in type类错误地实现了接口“OnChanges”。类型中缺少属性“ngOnChanges”
【发布时间】:2017-06-18 11:38:04
【问题描述】:

我基本上开始使用 angular 4 并使用 ngChanges 我得到了这个错误

类“GalleryComponent”错误地实现了接口 'OnChanges'.Property 'ngOnChanges' 类型缺失 '画廊组件'。

我真的不知道该怎么办,我知道一个类可以有多个接口,但事件只使用一个它会显示相同的错误

import { Component, OnInit, OnChanges, Input } from '@angular/core';
import { ImageService } from '../image/shared/image.service';

@Component({
  selector: 'app-gallery',
  templateUrl: './gallery.component.html',
  styleUrls: ['./gallery.component.css']
})


export class GalleryComponent implements OnInit, OnChanges {
visibleImages: any[] = [];

 constructor(private imageService: ImageService) {
  this.visibleImages = this.imageService.getImages();
 }

 ngOnInit() {}

 OnChanges(){}

}

如果有人能注意到我遗漏的东西,我将不胜感激 提前致谢

【问题讨论】:

  • 函数定义不是:ngOnChanges(changes: SimpleChanges) 而不是onChanges()
  • woooo 你是对的......那是错误,非常感谢你

标签: angular oop implements


【解决方案1】:

第一个问题是函数名:

OnChanges(){}
To
ngOnChanges(changes: SimpleChanges){}

你需要在ngOnChanges函数中添加参数changes: SimpleChanges

ngOnChanges(changes: SimpleChanges) {
    // changes.prop contains the old and the new value...
}

请阅读: https://angular.io/api/core/OnChanges

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2018-03-23
    • 2017-02-15
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多