Angular 2 service property values undefined in my lazy loaded modules -
i have several properties in service share in other lazy loaded modules
my service defined as:
@injectable() export class appsettingsservice { private _deviceid: string; private _accountid: string; constructor() { } initialise(): promise<any> { if(this.getdeviceidfromlocalstorage() && this.getaccountidfromlocalstorage()) { return promise.resolve(errorcode.existing_user); } } setapplicationid(applicationid): void { this._deviceid = applicationid; } getapplicationid(): string { return this._deviceid; } setaccountid(accountid): void { this._accountid = accountid; } getaccountid(): string { if(typeof this._accountid !== 'undefined') { return this._accountid; } else { return '0'; } } public getdeviceidfromlocalstorage(): boolean { let deviceid = localstorage.getitem(appkeys.device_id); if((typeof deviceid !== 'undefined') && (deviceid !== null)){ this.setapplicationid(deviceid); return true; } else { return false; } } public getaccountidfromlocalstorage(): boolean { let accountid = localstorage.getitem(appkeys.account_id); if((typeof accountid !== 'undefined') && (accountid !== null)){ this.setaccountid(accountid); return true; } else { return false; } } }
and declare service in shared module
import { ngmodule, modulewithproviders } '@angular/core'; import { commonmodule } '@angular/common'; import { formsmodule } '@angular/forms'; import { routermodule } '@angular/router'; import { logocomponent } './logo.component'; import { appsettingsservice } './app-settings.service'; @ngmodule({ imports: [ commonmodule ], declarations: [ logocomponent ], exports: [ commonmodule, formsmodule, routermodule, logocomponent ] }) export class sharedmodule { static forroot(): modulewithproviders { return { ngmodule: sharedmodule, providers: [ appsettingsservice ] } } }
all data loads fine in core app. when navigate different lazy loaded module, , call getapplicationid() or getaccountid() properties _deviceid , _accountid undefined.
in lazy loaded module, import shared module
import { ngmodule } '@angular/core'; import { commonmodule } '@angular/common'; import { httpmodule } '@angular/http'; import { sharedmodule } '../shared/shared.module'; <excluded clarity> @ngmodule({ imports: [ commonmodule, httpmodule, sharedmodule.forroot(), ], declarations: [ <excluded clarity> ], providers: [ <excluded clarity> ] }) export class advertisementmodule { }
in component, import service
import { appsettingsservice } '../shared/app-settings.service’;
and reference in constructor
constructor(private _router: router, private _appservice: appsettingsservice) { }
what missing or not possible persist values in service in way? i'm using angular-cli webpack 2.1.0-beta.25
Comments
Post a Comment