How to persist data when navigating from one page to another angular2 nativescript
Hi @sarvagayatri, Thank you for your interest in NativeScript.
In NativeScript Angular project you could use RouterConfig object which maps paths to components and you could past parameters while navigating as it is described in our documentation.
For further help you could review the Angular2 documentation, where you could find further info about the Angular router and, how you could parse data.
You could review this thread in SO here, where this scenario has been described.
You could also use our StackOverflow and Slack canals for your questions. I hope this helps. @tsonevn
I want to save few variable values through out the app, so that i can refer them in any page of app. so routing variables does't work for me. i want to persist few application state variables eg. userId, userStatus etc..
Maybe this ? https://docs.nativescript.org/api-reference/modules/application_settings.html
Hi @sarvagayatri,
You could use Application Settings as @AzSiAz has suggested, you could find an example how to use this module here in the documentation. You could also review the SO thread here, where has been described how to define global variables in Angular2.
In addition to what people have already brought up, the Angular router allows you to provide parameters for the component you are navigating to:
https://angular.io/docs/ts/latest/guide/router.html#!#route-parameters
What about back navigation params?
@sarvagayatri
you can use service in that case, routing will not fit. try like this.
PersistDataClass.ts
class PersistDataClass {
userData: Array<object>
}
Component where you want to set and later get.
import { PersistDataClass } from './PersistDataClass';
import { OnInit } from '@angular/core';
export class UserComponent implements OnInit() {
constructor(private _userservice: PersistDataClass) {
}
ngOnInit() {
service.getUserList().subscribe( res => {
_userservice.userData = res;
})
}
}