wly11132 2021-11-15 17:30 采纳率: 60%
浏览 1928
已结题

Type 'string | null' is not assignable to type 'string'.

img

在从localstrage取变量的时候赋值提示此报错 为什么?

TS2322: Type 'string | null' is not assignable to type 'string'.   Type 'null' is not assignable to type 'string'.

  • 写回答

4条回答 默认 最新

  • CSDN专家-showbo 2021-11-15 17:33
    关注

    localStorage中的authorName键不存在,返回null了,错误提示null无法赋值给string类型变量,做个兼容

    this.authorName=localStorage.getItem('authorName')||'';//为null则默认为空字符串
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月24日
  • 已采纳回答 11月16日
  • 创建了问题 11月15日