您好我正在使用Angular2 Web应用程序,After View Initialized我需要触发一个函数但我收到错误,我已经关注了官方angular2网站https://angular.io/docs/ts/latest/api/core/index/AfterContentInit-class.html
错误: EXCEPTION:未捕获(在承诺中):ReferenceError:未定义ngAfterViewInit ReferenceError:未定义ngAfterViewInit
代码:
import { Component } from '@angular/core' ;
import { DropDown } from '../services/dropdown';
import {ExponentialStrengthPipe} from '../exponential-strength.pipe';
import {HashLocationStrategy, Location, LocationStrategy} from '@angular/common';
@Component({
selector:'pageone',
templateUrl:'app/page1/page1.component.html',
providers:[DropDown,Location, {provide: LocationStrategy, useClass: HashLocationStrategy}]
})
export class Page1Component implements AfterViewInit
{
ind:number=1.5;
usa:number=1.5;
myd:any;
search:string;
mydetails:any;
constructor(private mydata:DropDown,location: Location){
this.myd = mydata.getData();
this.mydata.getMyprofile().subscribe(posts=>{
console.log(posts);
this.mydetails=posts;
});
ngAfterViewInit() {
// ...
}
}
addLocation(){
if(this.search=="" || this.search==undefined)
{
alert("Please enter loction");
}
else
{
for(var i=0;i<this.myd.length;i++)
{ var count=0;
if(this.myd[i].locationName==this.search)
{
alert("Already Present")
count=1;
}
}
if(count==0)
{
var id = this.myd.length+1;
this.myd.push({id:id,locationName:this.search});
alert(this.search+" Has Been Added ");
}
}
}
}
需要调用函数
ngAfterViewInit() {
// ...
}
答案 0 :(得分:5)
您应该从AfterViewInit
导入@angular/core
方法:
import { AfterViewInit } from '@angular/core';
而且你也错过了构造函数结束括号。
答案 1 :(得分:1)
});
} // <<<=== missing
ngAfterViewInit() {