performance-demo.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * 性能优化示例 - 展示自动导入的使用
  3. * 这个文件展示了如何使用自动导入的API,无需手动导入
  4. */
  5. // 使用自动导入的 Vue API(无需 import)
  6. export function usePerformanceDemo() {
  7. // 响应式数据
  8. const loading = ref(false);
  9. const data = reactive({
  10. list: [],
  11. total: 0
  12. });
  13. // 计算属性
  14. const isEmpty = computed(() => data.list.length === 0);
  15. // 获取路由信息
  16. const route = useRoute();
  17. const router = useRouter();
  18. // 本地存储
  19. const theme = useLocalStorage('theme', 'light');
  20. // 异步操作示例
  21. const fetchData = async () => {
  22. loading.value = true;
  23. try {
  24. // 使用自动导入的 axios
  25. const response = await axios.get('/api/data');
  26. data.list = response.data.list;
  27. data.total = response.data.total;
  28. // 使用自动导入的 message
  29. message.success('数据加载成功');
  30. } catch (error) {
  31. // 使用自动导入的 notification
  32. notification.error({
  33. message: '加载失败',
  34. description: error.message
  35. });
  36. } finally {
  37. loading.value = false;
  38. }
  39. };
  40. // 生命周期
  41. onMounted(() => {
  42. fetchData();
  43. });
  44. return {
  45. loading,
  46. data,
  47. isEmpty,
  48. theme,
  49. fetchData
  50. };
  51. }
  52. // 工具函数示例
  53. export function formatDate(date) {
  54. // 使用自动导入的 dayjs
  55. return dayjs(date).format('YYYY-MM-DD HH:mm:ss');
  56. }
  57. // 路由导航示例
  58. export function navigateToPage(path) {
  59. const router = useRouter();
  60. router.push(path);
  61. }