2016-05-16 6 views
9

が定義されていないのOnInitは、私は、角2のOnInit機能を実装しようとしているが、私は、コンソールに次のエラーを取得する:Angular2 -

Error: ReferenceError: OnInit is not defined(...)

誰かが私が間違ってやっていると指摘することはできますか?

これは私のコンポーネントです:

import { Component, OnInit } from 'angular2/core'; 
import { ViewEncapsulation } from 'angular2/core'; 

@Component({ 
    selector: 'Landing', 
    templateUrl: '/app/views/landing.html', 
    styleUrls: ['../app/styles/landing.css'], 
    encapsulation: ViewEncapsulation.None 
}) 

export class LandingComponent implements OnInit { 

    function checkOverflow() { 
     var element = document.querySelector('.appContainer'); 
     if((element.offsetHeight < element.scrollHeight) || (element.offsetWidth < element.scrollWidth)){ 
      // your element have overflow 
      console.log('OVERFLOW!'); 
     } 
     else{ 
      console.log('NO OVERFLOW'); 
     } 
    } 

    OnInit() { 
     checkOverflow(); 
    } 

} 

答えて

13

ではなく、2つのimport文のsingleインポートを使用してください。あなたはあなたのようにngOnInitメソッドを実装する必要があり

import { Component, OnInit, ViewEncapsulation } from '@angular/core'; 

(リリース候補)版をrc使用している場合にも、コンポーネントクラスimplements OnInitクラス

ngOnInit() { 
    this.checkOverflow(); 
} 

さらに方法でfunctionを使用していない@angular/core@を使用します現在typecriptファイルで使用していますが、checkOverflow(){ ... }フォーマット&に変換してからthis.checkOverflow()