Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there localstorage in nativescript?

How to share the data across the pages which resides in application.Can anyone tell about localstorage in nativescript?

like image 602
Naren Avatar asked Sep 13 '16 06:09

Naren


2 Answers

If you are using NS8, use "application-settings" from @nativescript/core

For example:

import * as appSettings from "@nativescript/core/application-settings";
...
appSettings.setString("token", response.token);
appSettings.setString("userId", response.user.id.toString());
appSettings.setString("email", email);
like image 179
Jose Chavez Avatar answered Nov 05 '22 14:11

Jose Chavez


If you install the "nativescript-localstorage" plugin,

tns plugin install nativescript-localstorage

You will then have access to both SessionStorage and LocalStorage just as if you were using a browser.


From the docs:

To set a value:

require( "nativescript-localstorage" );
localStorage.setItem('Another Plugin', 'By Master Technology');
localStorage.getItem('Another Plugin'); // Returns: "By Master Technology"

or

const LS = require( "nativescript-localstorage" );
LS.setItem('Another Plugin', 'By Master Technology');
LS.getItem('Another Plugin');  // Returns: "By Master Technology"

Disclaimer I'm the author of said plugin.

like image 30
Nathanael Avatar answered Nov 05 '22 12:11

Nathanael