0

ReferenceError: localStorage is not defined

File: "finle:///data/data/org.nativescript/dist/index.js. line 104, column: 40

StackTrace: Frame: function:'exports.localStorageSync',file:'file:///data/data/org.nativescript.MyApp/files/app/tns_modules/ngrx-store-localstorage/dist/index.js',line:104,column:41 Frame: finction:",file:'file:///data/data/org.nativescript.MyApp/files/app/core/store/index.js',line:19,column:69

Is it must to use https://www.npmjs.com/package/nativescript-localstorage or Can we use https://github.com/natural-apptitude/ngrx-store-ionic-storage or ngrx-store-localstorage

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • It may be possible to hack Cordova plugins to work with NativeScript, but you are much better using nativescript-localstorage. – Colin Skow May 21 '17 at 22:01

2 Answers2

2

NativeScript doesn't have LocalStorage. You have to install a shim.

tns plugin add nativescript-localstorage

Then at the top of your app.module.ts before you initialize ngrx:

import 'nativescript-localstorage';

This should allow ngrx-store-localstorage to work.

Read the docs at: https://github.com/NathanaelA/nativescript-localstorage

Colin Skow
  • 1,006
  • 12
  • 21
  • Now it throws this `JS: ERROR Error: No provider for Store! JS: ERROR CONTEXT [object Object] JS: ns-renderer: ERROR BOOTSTRAPPING ANGULAR JS: ns-renderer: No provider for Store! JS: JS: Error JS: at injectionError (file:///data/data/org.nativescript.MyApp/files/app/tns_modules/@angular/core/bundles/core.umd.js:1238:86) [angular]` – ishandutta2007 May 21 '17 at 22:43
  • Remove `ngrx-store-localstorage` and make sure your app is working without it. NativeScript has limited support for Node & browser APIs so getting packages to work can require a lot of hacking. Make sure you have @ngrx/store set up as specified in the docs: https://github.com/ngrx/store – Colin Skow May 21 '17 at 22:53
  • Well this is the fiirst thing I am adding, so rest works fine . I have moved it to a separate question http://stackoverflow.com/questions/44105335/no-provider-for-store-error-at-injectionerror – ishandutta2007 May 22 '17 at 05:38
0

you have to do three steps

1 install the plugin

tns plugin add nativescript-localstorage

2 import it in app.module.ts

import 'nativescript-localstorage';

3 use it in the corresponding component

import { Component, OnInit } from "@angular/core";
import { DataService, IDataItem } from "../core/data.service";
require( "nativescript-localstorage" );
@Component({
....
})

 export class HomeComponent implements OnInit {
 ......
}
Mujthaba
  • 25
  • 8