已定义变量,但控制台显示未定义
Variable defined, but the console says not
这是一个简单的 alertController 框,它创建了一个清单元素。当我点击添加时,class ChecksPage 的函数 addCheck() 被调用:
export class ChecksPage implements OnInit {
checkList: Check[] = [];
constructor(private alertCtrl: AlertController) { }
ngOnInit() {
}
addCheck(){
this.alertCtrl.create({
header: 'Nouvelle liste',
inputs:[{
name:'nom',
placeholder: 'Nom'
}],
buttons:[
{
text: 'Annuler',
role: 'cancel'
},
{
text: 'Ajouter',
handler: data => {
if(typeof data.nom !=null){
let newCheck: Check;
newCheck.id = ''+this.checkList.length;
newCheck.title = data.nom;
this.checkList.push(newCheck);
}
}
}]
}).then(alertEl => alertEl.present());
}
}
但是,我有一个错误:变量 newCheck 未定义。为什么 ?
检查是一个接口:
export interface Check{
id: string;
title: string;
}
你没有初始化你的变量,请按如下操作:
const newCheck = {
id: ''+this.checkList.length,
title: data.nom
} as Check;
这是一个简单的 alertController 框,它创建了一个清单元素。当我点击添加时,class ChecksPage 的函数 addCheck() 被调用:
export class ChecksPage implements OnInit {
checkList: Check[] = [];
constructor(private alertCtrl: AlertController) { }
ngOnInit() {
}
addCheck(){
this.alertCtrl.create({
header: 'Nouvelle liste',
inputs:[{
name:'nom',
placeholder: 'Nom'
}],
buttons:[
{
text: 'Annuler',
role: 'cancel'
},
{
text: 'Ajouter',
handler: data => {
if(typeof data.nom !=null){
let newCheck: Check;
newCheck.id = ''+this.checkList.length;
newCheck.title = data.nom;
this.checkList.push(newCheck);
}
}
}]
}).then(alertEl => alertEl.present());
}
}
但是,我有一个错误:变量 newCheck 未定义。为什么 ? 检查是一个接口:
export interface Check{
id: string;
title: string;
}
你没有初始化你的变量,请按如下操作:
const newCheck = {
id: ''+this.checkList.length,
title: data.nom
} as Check;