Typescript “之间的区别是什么?”;激活";及;恩戈尼尼特;?
我看到这个样品了 如果我这样做,我会调用Typescript “之间的区别是什么?”;激活";及;恩戈尼尼特;?,typescript,angular,Typescript,Angular,我看到这个样品了 如果我这样做,我会调用this.show()在ngOnInit中,而演示程序在activate中调用它 activate和ngOnInit之间有什么区别?谢谢 export class ToastComponent implements OnInit { // public properties message: string; title: string; // private fields private defaults = { title:
this.show()代码>在ngOnInit
中,而演示程序在activate
中调用它
activate
和ngOnInit
之间有什么区别?谢谢
export class ToastComponent implements OnInit {
// public properties
message: string;
title: string;
// private fields
private defaults = {
title: '',
message: 'May the Force be with You'
};
private toastElement: any;
// public methods
activate(message = this.defaults.message, title = this.defaults.title) {
this.title = title;
this.message = message;
this.show();
}
ngOnInit() {
this.toastElement = document.getElementById('toh-toast');
}
// private methods
private hide() {
this.toastElement.style.opacity = 0;
window.setTimeout(() => this.toastElement.style.zIndex = 0, 400);
}
private show() {
console.log(this.message);
this.toastElement.style.opacity = 1;
this.toastElement.style.zIndex = 9999;
window.setTimeout(() => this.hide(), 2500);
}
}
ngOnInit()
是一种角度生命周期方法,在第一次更新输入(第一次ngOnChanges()
之后)后调用activate
只是一个自定义方法,不是Angular调用的。要使用它,需要通过自定义代码调用。谢谢!!如果示例代码在ngOnInit()
内部调用了this.activate()
,那么它将被清除。感觉有点误导