【问题标题】:Angular 5 error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'Angular 5 错误 TS2345:“数字”类型的参数不可分配给“字符串”类型的参数
【发布时间】:2018-07-28 01:47:26
【问题描述】:

我需要如下设置一个cookie:

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

但我明白了:

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

我应该定义 cookie 变量类型还是什么?

【问题讨论】:

    标签: angular


    【解决方案1】:

    Cookie 以字符串格式存储。看起来this._cookieService.set 方法的签名接受字符串,所以你可以这样做:

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

    【讨论】:

    • 但我需要 this.start 是一个数字,以便我可以计算超时!
    • 属性本身在你的应用程序的其余部分仍然是一个数字。 String() 不会改变它,它只会将它转换为这个特定方法的字符串。
    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 2018-02-21
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2018-08-10
    • 2020-01-12
    相关资源
    最近更新 更多