useUniStore.ts 738 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // 同步取
  2. function getItem(key: string) {
  3. try {
  4. const value = uni.getStorageSync(key);
  5. if (value) {
  6. return value;
  7. }
  8. return null;
  9. } catch (e) {
  10. // error
  11. return null;
  12. }
  13. }
  14. // 同步存
  15. function setItem(key: string, data: string | boolean | number | Object) {
  16. uni.setStorageSync(key, data);
  17. }
  18. // 同步销毁
  19. function removeItem(key: string) {
  20. try {
  21. uni.removeStorageSync(key);
  22. } catch (e) {
  23. console.log('移除数据出错', e);
  24. }
  25. }
  26. // 同步清理所有
  27. function removeAll() {
  28. try {
  29. uni.clearStorageSync();
  30. } catch (e) {
  31. console.log('移除数据出错', e);
  32. }
  33. }
  34. export const useUniStore = () => {
  35. return {
  36. getItem,
  37. setItem,
  38. removeItem,
  39. removeAll
  40. }
  41. }