1
沒有人知道如何將Ionic2 Storage.get值分配給我的本地變量?如果我在.then中使用console.log,可以正常工作,但它似乎只存在於該函數/方法中。 大多數例子我看,說明如何「讓」我的數據,但沒有真正把它應用到我的其他代碼Ionic2 Storage Promise.all範圍
-Thanks
import { Storage } from '@ionic/storage';
export class MyApp {
favDessertMax: string;
favDessertRuby: string;
constructor(storage: Storage) { }
storage.ready().then(() => {
this.storage.set('Max', 'Apple sauce');
this.storage.set('Ruby', 'Banana split');
Promise.all([
this.storage.get('Max'),
this.storage.get('Ruby'),
])
.then(([val1,val2]) => {
this.favDessertMax = val1;
this.favDessertRuby = val1;
console.log(val1 + " " + val2); //values work here
})
console.log(val1 + " " + val2); // values don't work out here (or anywhere else)
});
storyTime() { // Need value to work here
let myStory = 'Max likes ' + this.favDessertMax + ' and Ruby Likes 'this.favDessertRuby';
return myStory;
}
}