vue3+view-ui-plus中封装全局loading

vue3+view-ui-plus中封装全局loading

import { Spin } from 'view-ui-plus'

// 将同一时刻的请求合并
let needLoadingRequersCount = 0;
export const showFullScreenLoading = function () {
  if (needLoadingRequersCount === 0) {
    Spin.show()
  }
  needLoadingRequersCount++
}
export const tryHideFullScreenLoading = function () {
  if (needLoadingRequersCount <= 0) {
    return
  }
  needLoadingRequersCount--
  if (needLoadingRequersCount === 0) {
    Spin.hide()
  }
}

### Vue 实现闲鱼网页的源代码或项目示例 Vue 是一个流行的前端框架,结合现代组件化开发模式,可以高效地构建复杂的单页应用(SPA)。以下是关于如何使用 Vue 实现类似闲鱼网页的功能和架构设计的详细说明。 #### 1. 项目技术栈 实现一个类似于闲鱼的网页,通常需要以下技术栈: - **Vue 3**:作为核心框架。 - **Vant** 或 **Element Plus**:用于 UI 组件库[^1]。 - **Vue Router**:管理页面路由[^2]。 - **Pinia** 或 **Vuex**:进行状态管理[^2]。 - **Axios**:处理 HTTP 请求[^2]。 - **SCSS/SASS** 或 **CSS Modules**:样式管理。 #### 2. 项目结构 一个典型的 Vue 项目结构可能如下所示: ```plaintext src/ ├── assets/ # 静态资源文件 ├── components/ # 可复用的组件 ├── views/ # 页面组件 │ ├── Home.vue # 首页 │ ├── ProductList.vue # 商品列表页 │ ├── ProductDetail.vue # 商品详情页 │ └── UserProfile.vue # 用户个人中心 ├── router/ # 路由配置 │ └── index.js ├── store/ # 状态管理 │ └── index.js ├── services/ # API 封装 │ └── api.js └── App.vue # 根组件 ``` #### 3. 主要功能模块 以下是实现闲鱼网页的主要功能模块及其代码示例: ##### (1) 底部导航栏 底部导航栏是闲鱼网页的核心功能之一,可以通过自定义组件实现。以下是一个简单的实现方式[^3]: ```vue <template> <view class="app"> <router-view></router-view> <tab-bar></tab-bar> </view> </template> <script> import TabBar from &#39;@/components/tab-bar.vue&#39;; export default { components: { TabBar }, }; </script> <style> .app { position: relative; height: 100vh; display: flex; flex-direction: column; } </style> ``` ##### (2) 商品列表页 商品列表页可以使用 Vant 的 `Card` 和 `List` 组件来快速实现[^1]: ```vue <template> <van-list v-model:loading="loading" :finished="finished" @load="onLoad"> <van-card v-for="product in products" :key="product.id" :price="product.price" :desc="product.desc" :title="product.title" :thumb="product.image" /> </van-list> </template> <script> import { ref } from &#39;vue&#39;; export default { setup() { const products = ref([]); const loading = ref(false); const finished = ref(false); const onLoad = () => { // 模拟异步请求数据 setTimeout(() => { for (let i = 0; i < 10; i++) { products.value.push({ id: products.value.length + 1, title: `商品 ${products.value.length + 1}`, desc: &#39;这是一个描述&#39;, price: Math.random() * 100, image: &#39;https://via.placeholder.com/150&#39;, }); } loading.value = false; if (products.value.length >= 40) { finished.value = true; } }, 1000); }; return { products, loading, finished, onLoad }; }, }; </script> ``` ##### (3) 商品详情页 商品详情页可以使用动态路由参数来加载不同的商品信息: ```vue <template> <div> <h1>{{ product.title }}</h1> <p>价格:{{ product.price }}</p> <img :src="product.image" alt="商品图片" /> <p>{{ product.desc }}</p> </div> </template> <script> import { ref, onMounted } from &#39;vue&#39;; import { useRoute } from &#39;vue-router&#39;; export default { setup() { const route = useRoute(); const product = ref({}); onMounted(async () => { // 模拟获取商品详情 const productId = route.params.id; product.value = await fetchProductById(productId); }); const fetchProductById = (id) => { return new Promise((resolve) => { setTimeout(() => { resolve({ id, title: `商品 ${id}`, price: Math.random() * 100, image: &#39;https://via.placeholder.com/150&#39;, desc: &#39;这是一个商品描述&#39;, }); }, 1000); }); }; return { product }; }, }; </script> ``` #### 4. 项目优化建议 - 使用 **懒加载** 提升首屏加载速度[^2]。 - 对 API 请求进行封装,统一处理错误和 Token[^2]。 - 使用 **Pinia** 进行全局状态管理,例如购物车、用户登录状态等[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值