Angular 2.0 lifecycle and Typescript
I have an Angular 2.0 component and I wanted to add lifecycle events to it (Angular 2.0 Alfa 35).
I was looking at this post as a reference, but Typescript gave me errors for using the
import {Component, View, EventEmitter, onInit} from 'angular2/angular2';
(error on onInit
). A quick look at Angular2 code revealed that the export uses OnInit
(with capital O
). I changed the import code but the lifecycle event itself is still onInit
. This is my component (I cannot make onInit event happen):
import {Component, View, EventEmitter, OnInit} from 'angular2/angular2';
@Component({
selector: 'theme-preview-panel-component',
properties: ['fontSize'],
lifecycle: [onInit]
})
@View({
templateUrl: '../components/theme-preview-panel-component/theme-preview-panel-component.tpl.html',
})
export class ThemePreviewPanelComponent {
fontSize: string;
constructor() {
}
onInit() {
//This code is not being called
}
}
EDIT
As @Eric Martinez mentioned below, the solution is:
import {Component, View, EventEmitter, LifecycleEvent} from 'angular2/angular2';
@Component({
selector: 'theme-preview-panel-component',
properties: ['fontSize'],
lifecycle: [LifecycleEvent.onInit]
})
@View({
templateUrl: '../components/theme-preview-panel-component/theme-preview-panel-component.tpl.html',
})
export class ThemePreviewPanelComponent {
fontSize: string;
constructor() {
}
onInit() {
//Now this code is being called
}
}
链接地址: http://www.djcxy.com/p/40688.html