Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 localstorage cannot find name

Tags:

angular

I am using angular CLI and I am trying to use localStorage for registration component, but this is no working, it says Cannot find name 'localStorage' This is my service

create(user: User) {
    return this.http.post('/signup', user, this.jwt(user)).map((response: Response) => response.json());
  }

  private jwt(user) {
    let currentUser = localStorage.setItem('currentUser', user);
    if (currentUser && currentUser.token) {
        let headers = new Headers({ 'Authorization': 'Bearer ' + currentUser.token });
        return new RequestOptions({ headers: headers });
    }
  }

how can I fix this?

like image 544
LLaza Avatar asked Aug 27 '26 08:08

LLaza


1 Answers

Maybe you have to add lib:dom in your tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

(source: tsconfig.json)

like image 156
Alexander K. J. Schmidt Avatar answered Aug 29 '26 09:08

Alexander K. J. Schmidt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!