在我的角度项目中,我有以下设置:角等待初始加载
我有API的应用程序负载的开头来从服务器的一些数据:
ItemFetch.ts
在应用程序加载开始时,它从API中提取数据,然后将itemLoaded
更改为true。
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
...
dataLoaded: boolean = false;
dataAPI(){ //Stores in local storage
...
.then(data=>{
this.itemLoaded = true;
})
...
}
main.ts:
然后,一旦数据被存储,我只需要加载存储数据时itemLoaded
从ItemFetch.ts
是真实的。
import { dataFromStorage} from './data_from_storage' //local storage
export class main_page {
constructor(public itemStorage: dataFromStorage){};
ngOnInit(){
this.fetchInitialData();
}
//Fetch the data from the storage
fetchInitialData(){
this.itemStorage.GetItemDataFromStorage('some_item_id')
.then((data) => {
console.log("Got the data!" + data);
)
};
}
问:
我如何从一个组件共享此dataLoaded
到另一个,这样我可以启动this.fetchInitialData();
只有当dataLoaded
是真的吗?
我会将变量存储在单例服务中。这样,两个组件都可以注入服务并使用它。 – LLai