简体   繁体   中英

Angular 5 error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'

I need to set a cookie as below:

start: number;
...
this.start = Math.floor(Date.now()/1000);
...
if(!this._cookieService.check('confirmTimeoutStarted')){
  this._cookieService.set('confirmTimeoutStarted', this.start);
}

But I get:

error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

Am I supposed to define the cookie variable type or something?

Cookies are stored in string format. It looks like the signature of the this._cookieService.set method accepts string, so you can do this:

this._cookieService.set('confirmTimeoutStarted', String(this.start));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM