vue3 解决各场景 loading过度-5中方法

1. 使用 v-if 和 v-show

使用场景:当需要根据条件显示或隐藏某个元素时,可以使用 v-if 和 v-show。

优点:代码简洁易懂,执行效率高。

缺点:当条件判断比较复杂时,v-if 和 v-show 的嵌套层数过多,可读性差。

示例代码:

<template>
  <div>
    <div v-if="isLoading">加载中...</div>
    <div v-else>内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: true,
    };
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false;
    }, 2000);
  },
};
</script>

2. 使用路由守卫(Navigation Guards)

使用场景:当需要在路由切换前后执行一些操作时,可以使用路由守卫。

优点:可以确保在路由切换过程中执行一些特定的逻辑,例如显示 loading 动画。

缺点:需要配合 Vue Router 使用,配置相对复杂。

示例代码:

import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next();
  } else {
    next({ path: '/' });
  }
});

export default router;

3. 使用异步组件(Async Components)

使用场景:当需要动态加载一个组件时,可以使用异步组件。

优点:可以提高应用程序的性能,因为只有在实际需要时才会加载组件。

缺点:需要对异步组件有一定的了解,才能正确地使用它们。

示例代码:

<template>
  <div>
    <AsyncComponent :is="isLoading" />
  </div>
</template>

<script>
import { defineAsyncComponent } from 'vue';

export default {
  data() {
    return {
      isLoading: true,
    };
  },
  components: {
    AsyncComponent: defineAsyncComponent(() => import('./AsyncComponent.vue')),
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false;
    }, 2000);
  },
};
</script>

4. 使用第三方库(如 element-plus、vant 等)提供的 loading 组件

使用场景:当需要提供更好的用户体验时,可以使用第三方库提供的 loading 组件。

优点:可以提供更美观、更易于使用的 loading 组件。

缺点:可能会增加项目的体积。

示例代码:

<template>
  <div>
    <el-loading v-if="isLoading" fullscreen></el-loading>
    <div v-else>内容</div>
  </div>
</template>

<script>
import { ElLoading } from 'element-plus';

export default {
  data() {
    return {
      isLoading: true,
    };
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false;
    }, 2000);
  },
  components: {
    ElLoading,
  },
};
</script>

5. 自定义全局的 loading 组件

使用场景:当需要提供更好的用户体验时,可以使用自定义的全局 loading 组件。

优点:可以提供更美观、更易于使用的 loading 组件。此外,自定义的 loading 组件可以更好地控制其显示和隐藏。

缺点:需要一定的开发成本。

MyLoading 的自定义组件:

<template>
  <div>
    <my-loading v-if="isLoading"></my-loading>
    <div v-else>内容</div>
  </div>
</template>

<script>
import MyLoading from './MyLoading.vue';

export default {
  data() {
    return {
      isLoading: true,
    };
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false;
    }, 2000);
  },
  components: {
    MyLoading,
  },
};
</script>
  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一花一world

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值