Angular 2.0生命周期和Typescript

我有一个Angular 2.0组件,我想添加生命周期事件(Angular 2.0 Alfa 35)。

我正在看这篇文章作为参考,但Typescript给我错误的使用

import {Component, View, EventEmitter, onInit} from 'angular2/angular2';

onInit错误)。 快速查看Angular2代码,发现导出使用OnInit (大写字母O )。 我改变了导入代码,但生命周期事件本身仍然是onInit 。 这是我的组件(我无法使onInit事件发生):

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
  }
}

编辑

正如@Eric Martinez所提到的,解决方案是:

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/40687.html

上一篇: Angular 2.0 lifecycle and Typescript

下一篇: TypeScript Converting a String to a number