我正在开发一个Ionic应用程序,在我的一个页面中,我已经定义了一组由结构化数据组成的数组(instanceStack)(产品,见下文)。在用于推送这些结构化数据集的函数(saveToStack)中,我可以在推送后访问数组中的数据。但是,这就是问题,如果我想从另一个方法(logInstances)再次访问它,它会显示为空白。
这是代码。
export class HomePage {
product = { name: '', code: '', quantity: 1, salesMessage: "", discountPercent: 0.00, discountCurrency: 0.00, price: 0, listID: "", saleDate: "", saleTime: "", isSale: true, locationID: ""};
instanceStack = [];
constructor(private loadingCtrl: LoadingController, private alertCtrl: AlertController, private navCtrl: NavController, private nativeStorage: NativeStorage, private barcode: BarcodeScanner, private dataService: DataProvider, private storageService: StorageProvider, private http: Http, private navParams: NavParams) {
}
private saveToStack(){
this.instanceStack.push(this.product);
console.log(this.instanceStack[0]['name']); //Can access just fine, it's printed to console as expected.
}
private logInstances(){
console.log("Instance Stack Length: "+ this.instanceStack.length); //As more things are pushed to instanceStack, this increases.
console.log(this.instanceStack[0]['name']); //Nothing gets printed (here's the problem)
}
这是什么问题?为什么没有在logInstances函数中将任何内容打印到控制台?