1
Angular 2 Style Guideでこのサンプルを見ました。"activate"と "ngOnInit"の違いは何ですか?
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()'の中で 'this.activate()'を呼び出すと、それは明らかです。一種の誤解を招く –