属性“令牌"在类型“对象"上不存在.打字稿问题 [英] Property 'token' does not exist on type 'Object'. Typescript issue

查看:92
本文介绍了属性“令牌"在类型“对象"上不存在.打字稿问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我收到一个编译器错误,指出在Object类型上不存在令牌.该对象与两个对象一起从服务器中返回.令牌和用户对象.我不确定如何在下面的代码示例中定义res,以便打字稿可以.

So I'm getting a compiler error stating that token doesn't exist on type Object. This object is getting returned from the server with two objects. A token and a user object. I'm not sure how to define res in the code example below so that typescript is okay with this.

return this.http.post(api.url, params)
        .map(res => {
          if (res && res.token) {
              localStorage.setItem('token', res.token);
              localStorage.setItem('user', JSON.stringify(res.user));
              return true;
          }

          return false;
        });

推荐答案

Typescript抱怨响应中没有名为token的属性,请将其更改为

Typescript is complaining the response does not have a property named token, Change it as,

return this.http.post(api.url, params)
        .map((res:any) => {

这篇关于属性“令牌"在类型“对象"上不存在.打字稿问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆