# vite-uniapp-template
🚀 基于 vitejs 驱动的 uniapp 最佳实践集成模板
🚀 The best practical integrated template based on VITEJS -driven Uniapp
[查看演示](https://vite-uniapp-template.netlify.app/)
## 特点
- 💪 Assets: 提供了全局静态资源加载工具,无感切换加载本地静态资源/远程静态资源,解决小程序环境下包大小限制问题。
- 📦 SubPackages: 符合心智模型的分包风格,合理的 pages 目录结构,与分包配置轻松实现功能分包。
- 🛣 Router: 使用 uniapp-router-next,并通过优化封装,API 同 VueRouter 类似,扩展了拦截器、中间件、路由别名功能。
- 📊 Store: 使用 Pinia 强力驱动,轻松实现应用状态管理。
- ⚡️ Request: 请求库使用 uni-network 完全基于 uniapp API 编写的高性能请求库, API 同 axios。
- 👇 Z-paging: 内置了高性能且易于使用的业务常用下拉分页组件模块,轻松实现下拉刷新、上拉加载等功能。
- 💅 Unocss: 使用原子化 CSS 引擎,小程序环境下依然完美支持原子化的 class 命名书写规则。
- 🎨 UI-libs: 预设了 uivew-plus 和 uni-ui 两者相辅相成,轻松满足绝大多数业务场景,并支持主题色定制功能。
- 📝 NoTs: 只使用 JavaScript,在常规业务场景或人员水平差距过大情况下,TypeScript 并不会提升开发体验。
## 使用方法
### 克隆项目
```shell
git clone https://github.com/viarotel/vite-uniapp-template.git
```
### 安装项目依赖
> 打开并进入克隆的项目根目录下执行以下命令
> 以下命令推荐 使用 pnpm 进行操作 不过你依然可以使用 npm/yarn
```shell
pnpm install
```
### 运行项目
#### 任意编辑器直接运行本项目
```shell
# h5端
pnpm dev:h5
# 微信小程序端
pnpm dev:mp-weixin
#... 更多端请参阅 package.json/script
```
#### 在 HBuilder 中运行本项目
1. 将项目拖动到 HBuilder 中
2. 使用 pnpm install 安装好依赖
3. 点击项目 /src 目录中的任意文件
4. 点击编辑器上方点击运行选择需要运行的环境
### 功能示例
#### 静态资源处理
```js
// 使用远程静态资源
import { useAssets } from './utils/assets/remote'
// 使用本地静态资源
import { useAssets } from './utils/assets/local'
// 全局挂载
app.config.globalProperties.$assets = useAssets
// template中使用
// <img :src="$assets('/temp.png')" />
```
#### 全局主题色定制
> 由 [unocss-preset-shades](https://github.com/viarotel-org/packages/tree/main/packages/unocss-preset-shades#readme) 提供支持
```html
<!-- 设置文本色为主题色色调为 500 -->
<div class="text-primary-500"></div>
<!-- 设置背景色为主题色色调为 500 -->
<div class="bg-primary-500"></div>
<!-- 设置边框色为主题色色调为 500 -->
<div class="border border-primary-500"></div>
<!-- 更多使用方式请参阅 https://tailwindcss.com/docs -->
```
#### 路由间功能跳转
```js
// 跳转页面
const methods = {
routerDemo() {
this.$Router.push({
path: '/login',
query: {
id: 'id',
},
})
// 获取页面参数
this.$Route.query.id
// 关闭当前页面跳转到某个页面
this.$Router.replace('/login')
// 关闭所有打开的页面跳转到某个页面
this.$Router.replaceAll('/login')
},
}
// 为路由设置别名
// pages.config.js 中
const aliasConfig = {
path: 'pages/login/index',
// 通过添加 aliasPath 字段
aliasPath: '/login',
}
```
#### 使用路由守卫
> 位于 router/guards 中
```js
import store from '@/store/index.js'
const homePath = '/pages/index/index'
const loginPath = '/pages/login/index'
const whiteList = [loginPath]
export default (router) => {
const userStore = store.useUserStore()
const loginRoute = to => ({
path: loginPath,
navType: 'reLaunch',
force: true,
query: {
redirect: {
path: to.path,
query: to.query,
},
},
})
router.beforeEach((to, from, next) => {
// console.log('permission.beforeEach.to', to)
// console.log('permission.beforeEach.from', from)
const token = userStore.token
const userId = userStore.userId
console.log('token', token)
console.log('userId', userId)
if (token) {
if (to.path === loginPath) {
next(homePath)
}
else if (userId) {
next()
}
else {
userStore
.getUserInfo()
.then(() => {
next()
})
.catch((error) => {
console.warn(error)
userStore.logout({ silenced: true })
next(loginRoute(to))
})
}
}
else if (whiteList.includes(to.path)) {
next()
}
else {
next(loginRoute(to))
}
})
router.afterEach(() => {})
}
```
#### 使用基于路由的中间件
> pages.config.js 中
```js
// 使用名为 realname 的中间件
const pageConfig = {
path: '/pages/personal/index',
aliasPath: '/personal',
meta: {
middleware: ['realname'],
},
}
```
定义中间件
> router/guards/index.js 中
```js
// 使用 defineMiddleware 定义并包装为中间件
import realname from './realname/index.js'
import { defineMiddleware } from '$uni-router/middleware'
export default (app, router) => {
// 使用 defineMiddleware 定义了路由中间件
defineMiddleware(realname, { router, app })
}
```
编写路由中间件代码
> router/guards/realname/index.js 中
```js
import store from '@/store/index.js'
import { useDialog, useToast } from '@/utils/modals'
export default (router) => {
const userStore = store.useUserStore()
router.beforeEach((to, from, next) => {
console.log('realname.beforeEach.to', to)
console.log('realname.beforeEach.from', from)
const realStatus = userStore.userInfo.realStatus
switch (realStatus) {
case 3:
next()
break
case 2:
useToast('实名审核中, 请稍后再试').then(() => {
next(false)
})
break
case 4:
useDialog(`${userStore.userInfo.auditResult || '提交的实名信息不符'}`, {
title: '实名失败',
showCancelButton: true,
confirmText: '重新认证',
})
.then(() => {
next({ path: '/pages/realname/index' })
})
.catch(() => {
next(false)
})
break
default:
useDialog('请先进行实名认证', { showCancelButton: true })
.then(() => {
next({ path: '/pages/realname/index' })
})
.catch(() => {
next(false)
})
break
}
})
// router.afterEach(() => {})
}
```
### 主要使用的包
- vitejs
- uniapp
- pinia
- uview-plus
- uni-ui
- @uni-helper/uni-network
- uniapp-router-next
- z-paging
- unocss
- unocss-applet
### 常见问题
#### 无法正常安装依赖/无法启动
删除 pnpm-lock.yaml / yarn.lock / package-lock.json 文件后重新安装依赖
### 获得帮助
> 因为是开源项目 全靠爱发电 所以支持有限 更新节奏不固定
>
> 注意:非 BUG 或计划外的需求,有偿处理;至于金额,根据问题难易程度,你觉得帮助了多少,看着给吧(维护这些项目已经耗费了大量精力,还要免费花时间解答问题就说不过去了吧...所以白嫖的一律不通过。)
- issues: [issues](https://github.com/viarotel-org/vite-uniapp-template/issues)
- email: viarotel@qq.com
- weixin: viarotel
- qq: 523469508
### 支持项目
> 如果该项目帮到你的话,可以请我喝杯咖啡,让我更有精神完善该项目😛
<div style="display:flex;">
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/79dcbc40246743e2b6870419e88e0392~tplv-k3u1fbpfcp-watermark.image?" alt="payment-weixin" style="width: 45%;">
<img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1e5e69b83dd746deade
没有合适的资源?快使用搜索试试~ 我知道了~
基于vite驱动的uniapp最佳实践的集成模板,提供了全局静态资源加载工具
共123个文件
js:55个
vue:23个
png:12个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 60 浏览量
2023-10-18
09:10:18
上传
评论
收藏 754KB ZIP 举报
温馨提示
运行项目 任意编辑器直接运行本项目 # h5端 pnpm dev:h5 # 微信小程序端 pnpm dev:mp-weixin #... 更多端请参阅 package.json/script 在 HBuilder 中运行本项目 将项目拖动到 HBuilder 中 使用 pnpm install 安装好依赖 点击项目 /src 目录中的任意文件 点击编辑器上方点击运行选择需要运行的环境
资源推荐
资源详情
资源评论
收起资源包目录
基于vite驱动的uniapp最佳实践的集成模板,提供了全局静态资源加载工具 (123个子文件)
iconfont.css 3KB
github-dark.min.css 1KB
atom-one-light.css 856B
atom-one-dark.css 856B
applet.css 779B
restyle.css 306B
index.css 51B
overrides.css 43B
.eslintignore 75B
avatar.gif 377KB
.gitignore 284B
.gitkeep 0B
index.html 712B
bg-tel.jpg 67KB
uni-highlight.min.js 198KB
markdown-it.min.js 93KB
html-parser.js 10KB
treeSimple.js 5KB
tree.js 4KB
index.js 4KB
helper.js 4KB
index.js 4KB
pages.config.js 3KB
index.js 3KB
index.js 2KB
helpers.js 2KB
vite.config.js 2KB
uniapp.js 2KB
helper.js 1KB
index.js 1KB
index.js 1KB
main.js 1KB
index.js 1KB
index.js 1KB
index.js 1KB
unocss.config.js 1016B
showDictLabel.js 939B
index.js 916B
request.js 908B
index.js 898B
index.js 835B
index.js 800B
index.js 623B
index.js 614B
sessionStorage.js 562B
localStorage.js 552B
uniStorage.js 507B
cookieStorage.js 469B
index.js 446B
middleware.js 422B
index.js 418B
remote.js 373B
index.js 323B
index.js 318B
local.js 297B
index.js 274B
index.js 260B
postcss.config.js 227B
token.js 226B
.eslintrc.js 222B
index.js 204B
devServer.js 139B
index.js 134B
index.js 133B
index.js 102B
index.js 100B
index.js 100B
index.js 93B
index.js 28B
package.json 4KB
pages.json 4KB
manifest.json 2KB
package.json 2KB
settings.json 430B
package.json 412B
jsconfig.json 235B
LICENSE 1KB
README.md 8KB
CHANGELOG.md 7KB
readme.md 2KB
readme.md 1KB
changelog.md 733B
.npmrc 62B
image-wechat.png 61KB
icon-camera.png 22KB
avatar.png 21KB
bg-certificate-portrait.png 12KB
bg-certificate-badge.png 11KB
logo.png 4KB
tab-personal-active.png 2KB
tab-personal.png 2KB
tab-home-active.png 1KB
tab-example-active.png 1KB
tab-example.png 1KB
tab-home.png 1KB
uni.scss 2KB
theme.scss 1KB
zb-dropdown-item.vue 10KB
zb-dropdown-menu.vue 9KB
ua-markdown.vue 9KB
共 123 条
- 1
- 2
资源评论
云哲-吉吉2021
- 粉丝: 3214
- 资源: 1130
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功