Angular 类型为'的参数;正确';不可分配给类型为';字符串';安圭拉5

Angular 类型为'的参数;正确';不可分配给类型为';字符串';安圭拉5,angular,angular5,Angular,Angular5,我是angular的新手,我正在尝试为我的应用程序创建一个登录名。当我想要设置cookie变量时,我得到以下结果: ERROR in src/app/components/login/confirm/confirm.component.ts(113,51): error TS23 45: Argument of type 'true' is not assignable to parameter of type 'string'. 跟踪错误位置,我得到代码: this._cookies.se

我是angular的新手,我正在尝试为我的应用程序创建一个登录名。当我想要设置cookie变量时,我得到以下结果:

ERROR in src/app/components/login/confirm/confirm.component.ts(113,51): error TS23 45: 
Argument of type 'true' is not assignable to parameter of type 'string'.
跟踪错误位置,我得到代码:

this._cookies.set('waitingForConfCode', true);
我还有以下问题:

this._cookies.set('remainingTime', 60);
它不应该是数字,而应该是字符串

为什么呢?我的意思是我应该总是将cookie变量定义为字符串吗?我需要继续铸造它们还是有更好的方法来处理它


请在这方面帮助我。

如果我理解您的意思,那么您需要设置cookie参数,它应该是字符串。在这种情况下,请使用以下内容:

this._cookies.set('waitingForConfCode', 'true');
而不是布尔变量

编辑

通过查看angular.io doc,我了解到angular接受cookoies作为string:string值:

构造函数(_-cookieName:string='XSRF-TOKEN',_-headerName:string='X-XSRF-TOKEN')


我知道这样很好用。但是有没有办法将它们存储为非字符串值呢?@goseo理论上,你可以尝试类似这样的模拟:let cookieInt:any;cookie=60;然后将其传递给cookie值,但我不能100%确定它是否有效。@goseo另外,请尝试使用JSON.stringify。我的意思是,以您想要的方式存储它,但是当您设置cookie时,使用它,比如:JSON.stringify(cookieValue)@帕维尔,谢谢你。我想我会先试试你的想法comment@goseo你欢迎。如果第二种方法不起作用,也可以试试。无论如何,祝你好运:)