storage.ts 558 B

1234567891011121314151617181920212223242526272829303132333435
  1. // storage.ts
  2. /**
  3. * 存储数据
  4. */
  5. export const setItem = (key: string, value: any) => {
  6. value = JSON.stringify(value);
  7. localStorage.setItem(key, value);
  8. };
  9. /**
  10. * 获取数据
  11. */
  12. export const getItem = (key: string) => {
  13. try {
  14. let data = localStorage.getItem(key);
  15. return JSON.parse(data);
  16. } catch (err) {
  17. return null;
  18. }
  19. };
  20. /**
  21. * 删除指定数据
  22. */
  23. export const removeItem = (key: string) => {
  24. localStorage.removeItem(key);
  25. };
  26. /**
  27. * 删除所有数据
  28. */
  29. export const removeAllItem = () => {
  30. localStorage.clear();
  31. };