为什么在处理localStorage对象时需要json.parse和json.stringify? 如果我们在下面的代码中不使用JSON.parse和JSON.stringify(value)怎么办?
在Get方法中,为什么不能说localStorage.getItem(key) as T;
export class StorageService
{
constructor() { }
public Get<T>(key: string): T
{
return JSON.parse(localStorage.getItem(key)) as T;
}
public Set(key: string, value: any): void
{
localStorage.setItem(key, JSON.stringify(value));
}
public Clear()
{
localStorage.clear();
}
public Remove(key: string)
{
localStorage.removeItem(key);
}
}