uni-app全局文件配置与微信小程序开发实战

uni-app全局文件配置与微信小程序开发实战
1. uni-app全局文件体系深度解析作为一款基于Vue.js的跨平台开发框架uni-app的全局文件体系是项目初始化的骨架结构。这些文件共同构成了应用的基础配置、全局样式和行为控制。在实际开发中合理配置这些文件能显著提升开发效率和项目可维护性。全局文件主要包括pages.json路由配置与页面样式manifest.json应用配置与原生能力App.vue根组件与全局样式main.js入口文件与全局注册这些文件各司其职又相互配合构成了uni-app项目的四梁八柱。下面我将结合实战经验详细拆解每个文件的最佳实践。2. pages.json配置全攻略2.1 基础路由配置pages.json是uni-app的路由中枢采用JSON格式声明应用的所有页面路径和窗口表现。典型配置如下{ pages: [ { path: pages/index/index, style: { navigationBarTitleText: 首页, enablePullDownRefresh: true } }, { path: pages/user/user, style: { navigationBarTitleText: 个人中心 } } ] }关键点数组第一项默认为应用启动页调整页面顺序即可修改启动页。2.2 全局样式与页面级覆盖通过globalStyle设置全局窗口样式单个页面可覆盖全局设置{ globalStyle: { navigationBarTextStyle: black, navigationBarTitleText: uni-app, backgroundColor: #F8F8F8 }, pages: [ { path: pages/index/index, style: { navigationBarTitleText: 首页 // 覆盖全局标题 } } ] }实测建议优先在globalStyle设置通用样式页面独有样式在各自style中配置微信小程序中navigationBarBackgroundColor需使用十六进制颜色值2.3 条件编译技巧针对不同平台的条件编译示例{ pages: [ // #ifdef H5 { path: pages/web-only/web-only }, // #endif // #ifdef MP-WEIXIN { path: pages/wechat/wechat } // #endif ] }3. manifest.json原生配置详解3.1 基础应用配置manifest.json控制应用名称、版本等元信息{ name: MyApp, appid: __UNI__XXXXXX, versionName: 1.0.0, versionCode: 100, description: 应用描述 }3.2 各平台差异化配置通过mp-weixin等节点配置平台特有参数{ mp-weixin: { appid: wx123456789, setting: { urlCheck: false }, usingComponents: true } }避坑指南微信小程序需在此配置合法域名否则网络请求会被拦截。3.3 权限配置实战以获取地理位置权限为例{ mp-weixin: { permission: { scope.userLocation: { desc: 你的位置信息将用于定位服务 } } } }4. App.vue全局控制中心4.1 生命周期管理App.vue中可监听应用生命周期script export default { onLaunch(options) { console.log(App Launch, options) // 实现双token方案 this.initAuth() }, methods: { initAuth() { // 检查refreshToken有效性 // 静默刷新accessToken } } } /script4.2 全局样式方案在App.vue中定义全局CSSstyle /* 官方推荐使用uni.css */ import /common/uni.css; /* 自定义全局样式 */ page { background-color: #f5f5f5; font-size: 16px; } /style经验分享避免在App.vue写过多业务逻辑全局样式优先使用class选择器字体图标建议在此引入5. main.js入口文件优化5.1 全局组件注册高效注册全局组件方案import Vue from vue import uView from uview-ui // 自动化全局组件注册 const requireComponent require.context( /components, true, /[\w-]\.vue$/ ) requireComponent.keys().forEach(fileName { const componentConfig requireComponent(fileName) const componentName fileName.split(/).pop().replace(/\.\w$/, ) Vue.component(componentName, componentConfig.default || componentConfig) }) Vue.use(uView)5.2 全局过滤器与混入扩展Vue原型方法// 时间格式化过滤器 Vue.filter(dateFormat, value { return new Date(value).toLocaleString() }) // 全局混入 Vue.mixin({ methods: { $showToast(title) { uni.showToast({ title, icon: none }) } } })6. 微信小程序双Token实战6.1 方案设计思路针对微信小程序的token刷新策略登录获取accessToken(2h过期)和refreshToken(7天过期)接口请求携带accessToken当accessToken过期时用refreshToken获取新accessToken重发原始请求refreshToken过期则跳转登录页6.2 请求拦截器实现在main.js中配置import axios from axios const service axios.create({ baseURL: process.env.VUE_APP_BASE_API }) // 请求拦截 service.interceptors.request.use(config { config.header[Authorization] getAccessToken() return config }) // 响应拦截 service.interceptors.response.use( response response, async error { const { config, status } error.response if (status 401 !config._retry) { config._retry true await refreshToken() return service(config) } return Promise.reject(error) } )6.3 安全注意事项token存储建议使用uni.setStorageSync敏感操作需二次验证定期检查refreshToken有效期7. 全局文件调试技巧7.1 常见问题排查问题现象可能原因解决方案页面路由失效pages.json配置错误检查path是否对应实际路径样式不生效选择器优先级问题添加!important或调整结构插件未生效manifest未正确配置检查各平台节点配置7.2 性能优化建议按需配置页面预加载规则合理使用subPackages分包加载全局组件使用懒加载经过多个项目的实践验证这套全局文件配置方案能显著提升开发效率和项目可维护性。特别是在处理微信小程序这类严格限制包大小的平台时合理的全局配置可以节省大量调试时间。

最新新闻

日新闻

周新闻

月新闻