1.引入OnChanges
import { Component, OnInit, Input, OnChanges } from '@angular/core';
2.继承接口
export class AlertComponent implements OnInit, OnChanges {
}
3.声明ngOnChanges
ngOnChanges() {
}
4.整体代码
import { Component, OnInit, Input, Output, EventEmitter, OnChanges } from '@angular/core';
@Component({
selector: 'app-alert',
templateUrl: './alert.component.html',
styleUrls: ['./alert.component.css']
})
export class AlertComponent implements OnInit, OnChanges {
@Input() showPop: boolean;
@Input() showTips: any;
constructor() {
}
ngOnInit() {
}
ngOnChanges() {
if (this.showPop) {
var that = this
window.setTimeout(function () {
that.showPop = false;
that.showTips = ""
}, 2000);
}
}
}