我正在尝试实现angular 2 onInit函数,但在控制台中收到以下错误:
错误:ReferenceError:未定义OnInit(...)
有人可以指出我做错了什么吗?
这是我的组件:
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();
}
}
使用single
import而不是两个import语句。如果您正在使用(发行候选版本)版本,也请@
在之前@angular/core
使用rc
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
您应该ngOnInit
在组件类implements OnInit
类中实现方法
ngOnInit() {
this.checkOverflow();
}
另外,不要function
以您当前在打字稿文件中使用的方式使用,请将其转换为checkOverflow(){ ... }
格式,然后像调用它一样this.checkOverflow()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句