Angular 全局var值don';不变
我在一个项目中工作,我在一个离子组件(tab2Page)中使用一个名为cards的全局变量。我在app.component.html中使用了一个菜单,但在这个菜单中我有一个带有函数的项。这个函数,我称之为app.component.ts。但我认为它不会改变全球var的价值。这是我使用的好方法吗 功能Angular 全局var值don';不变,angular,ionic-framework,global-variables,return-value,Angular,Ionic Framework,Global Variables,Return Value,我在一个项目中工作,我在一个离子组件(tab2Page)中使用一个名为cards的全局变量。我在app.component.html中使用了一个菜单,但在这个菜单中我有一个带有函数的项。这个函数,我称之为app.component.ts。但我认为它不会改变全球var的价值。这是我使用的好方法吗 功能 addNewCardWithDistrict(arrondissement) { //this.cards = []; var cardsDistrict = []; var database =
addNewCardWithDistrict(arrondissement) {
//this.cards = [];
var cardsDistrict = [];
var database = firebase.firestore();
database.collection('Bar').where('arrondissement',"==", arrondissement)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
cardsDistrict.push(doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
//console.log(cardsDistrict);
this.cards = cardsDistrict;
//this.disabledMenu();
app.componenent.html
<ion-menu side="start" menuId="first" contentId="main" type="overlay">
<ion-header>
<ion-toolbar translucent>
<ion-title>Choix de l'arrondissement</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item>
<ion-menu-button color="primary" (click)="firstComponentFunction(1)">1er</ion-menu-button>
</ion-item>
<ion-item>
<ion-menu-button color="primary" (click)="firstComponentFunction(2)">2ème</ion-menu-button>
</ion-item>
</ion-list>
</ion-content>
</ion-menu>
<ion-router-outlet id="main"></ion-router-outlet>
</ion-app>
this.cards=cardsDistrict
必须在“then”函数中:.then(函数(querySnapshot){..here..}
.FTM,在TypeScript中使用“let”或“constant”在Angular应用程序中,不建议使用全局变量声明时态变量。请查看,以了解如何使用服务存储将由不同页面/组件共享的变量。是的,我将其放入then函数中,但他未找到属性this.cards=cardsDistrict;
必须位于“then”中函数:.then(函数(querySnapshot){..here..}
.FTM,在TypeScript中使用“let”或“constant”在Angular应用程序中,不建议使用全局变量声明时间变量。请查看,以了解如何使用服务存储将由不同页面/组件共享的变量。是的,我将其放入then函数中,但他找不到属性
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private tab : Tab2Page
) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
firstComponentFunction(number){
this.tab.addNewCardWithDistrict(number);
}
}```