So I am working on an Ionic app and have a lot of variable declarations inside my class (WelcomeAunthenticatedUser.ts) and those variables are used in my class functions. But I was trying to declare all those vars in an external .ts file and export that file and import it in my class. This is Is there any way to do that?
This is the Variables.ts file which I want to export into my class.
export var Variables: {
currentUser: string
scannedCode: string
currentAccessToken: string
imageUrl: string
product: number
cartCode: number
productQuantity: 1
//barcodeDataFields Vars
barcodeDataFieldsName: string
barcodeDataFieldsProductCode: string
barcodeDataFieldsDescription: string
barcodeDataFieldsImage: string
barcodeDataFieldsPrice: string
//productCart Vars
Quantity: number
Status: string
// userCart Vars
userCartName: string
userCartCode: number
userCartProductCode: number
userCartPrice: number
userCartTotalPrice: number
userCartQuantity: number
userCartEntryNumber: number
userCartVisibility: boolean
showButton: boolean
quantity: number
}
And I am importing it in my Welcome class:
import { Variables } from './../../constants/variables'
import { Paths } from './../../constants/constants'
import { ToastController } from '@ionic/angular';
import { TranslateService } from '@ngx-translate/core';
import { AlertController } from '@ionic/angular';
@Component({
selector: 'app-welcome-authenticated-user',
templateUrl: './welcome-authenticated-user.page.html',
styleUrls: ['./welcome-authenticated-user.page.scss'],
})
export class WelcomeAuthenticatedUserPage implements OnInit {
productQuantity = 1;
ngOnInit() {
Variables.currentUser = this.localStorage.retrieve(LocalStorage.USERNAME);
Variables.currentAccessToken = this.localStorage.retrieve(LocalStorage.ACCESS_TOKEN);
Variables.cartCode = this.localStorage.retrieve(LocalStorage.CART_CODE);
Variables.showButton = false;
}
And when I run it, it shows an error like this:
Toast Called
core.js:6014 ERROR TypeError: Cannot read property 'cartCode' of undefined
at WelcomeAuthenticatedUserPage.ionViewWillEnter (welcome-authenticated-user.page.ts:32)
at HTMLElement.handler (ionic-angular.js:2075)
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:465)
at invokeTask (zone-evergreen.js:1603)
at HTMLElement.globalZoneAwareCallback (zone-evergreen.js:1629)
at lifecycle (index-157155ee.js:148)
core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'currentUser' of undefined
TypeError: Cannot read property 'currentUser' of undefined
at WelcomeAuthenticatedUserPage.<anonymous> (welcome-authenticated-user.page.ts:50)
at Generator.next (<anonymous>)
Any way I could do this?