[英]Angular2 - OnInit is not defined
I'm trying to implement the angular 2 onInit function but I get the following error in the console: 我正在尝试实现angular 2 onInit函数,但我在控制台中收到以下错误:
Error: ReferenceError: OnInit is not defined(...)
错误:ReferenceError:未定义OnInit(...)
Can somebody point out what I'm doing wrong? 有人可以指出我做错了什么吗?
This is my component: 这是我的组成部分:
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();
}
}
Use single
import instead of two import statements. 使用
single
导入而不是两个import语句。 Also use @
before @angular/core
if you are using rc
(release candidate) version 如果使用
rc
(候选版本)版本,也可以在@angular/core
之前使用@
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
You should implement ngOnInit
method as you component class implements OnInit
class 您应该在组件类
implements OnInit
类时实现ngOnInit
方法
ngOnInit() {
this.checkOverflow();
}
Additionally don't use function
in the way you are currently using in typescript file, do convert it to checkOverflow(){ ... }
format & then call it like this.checkOverflow()
另外,不要以你当前在
checkOverflow(){ ... }
文件中使用的方式使用function
,将它转换为checkOverflow(){ ... }
格式然后像this.checkOverflow()
一样调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.