代码:
get(key: string){
// return 'this is a service';
return JSON.parse(localStorage.getItem(key));
}
编译错误:
(method) Storage.getItem(key: string): string | null
Returns the current value associated with the given key, or null if the given key does not exist in the list associated with the object.
Argument of type 'string | null' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.ts(2345)
View Problem (Ctrl+.)
No quick fixes available
修改如下调试通过:
get(key: string){
// return 'this is a service';
return JSON.parse(localStorage.getItem(key) || '{}');
}