选择时间:{{ formatTime(selectTime) }}
export default {
data() {
return { selectTime: new Date().getTime() }
},
methods: {
formatTime(time) {
return new Date(time).toLocaleString().replace(/:/g, '-')
},
onTimeChange(e) {
this.selectTime = e.detail.value // 精准获取时间戳
}
}
}
避坑指南:处理picker组件时,建议通过moment.js统一时间格式处理,避免不同平台时间解析差异
// #ifdef APP-PLUS
// 安卓返回键处理
plus.key.addEventListener('backbutton', () => {
if (this.canGoBack()) {
uni.navigateBack()
} else {
uni.showModal({ content: '是否退出应用?' })
}
})
// #endif
// #ifdef H5
// H5端特殊样式
import './styles/h5-only.css'
// #endif
▶️ CSDN 技巧:在代码块添加平台标签注释,提升内容辨识度
/* 微信小程序穿透样式 */
/deep/ .wx-only-class {
color: #409eff;
font-size: 28rpx;
}
/* 兼容H5写法 */
.h5-only-class {
color: #409eff !important;
font-size: 14px !important;
}
注意:支付宝小程序需使用::v-deep,建议建立平台样式映射表统一管理
// request.js核心代码
export function createRequest() {
let loadingTimer = null
const request = (options) => {
const { url, method = 'GET', showLoading = true } = options
if (showLoading) {
uni.showLoading({ title: '加载中...' })
loadingTimer = setTimeout(() => {
uni.hideLoading()
uni.showToast({ title: '请求超时', icon: 'none' })
}, 10000)
}
return uni.request({
...options,
success: (res) => {
clearTimeout(loadingTimer)
uni.hideLoading()
if (res.statusCode !== 200) {
throw new Error(res.errMsg)
}
return res.data
},
fail: (err) => {
clearTimeout(loadingTimer)
uni.hideLoading()
uni.showToast({ title: err.errMsg || '网络请求失败', icon: 'none' })
return Promise.reject(err)
}
})
}
return request
}
✅ 最佳实践:通过Vue.prototype.$http = createRequest()挂载全局,实现this.$http直接调用
// 带过期时间的缓存工具
const CACHE_EXPIRE = 3600 // 1小时有效期
function setCache(key, value) {
uni.setStorageSync(key, JSON.stringify({
value,
expireTime: Date.now() + CACHE_EXPIRE * 1000
}))
}
function getCache(key) {
const cache = uni.getStorageSync(key)
if (!cache) return null
const { value, expireTime } = JSON.parse(cache)
return Date.now() < expireTime ? value : null
}
Vue.directive('lazy', {
inserted: function (el, binding) {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
el.src = binding.value
observer.unobserve(el) // 加载后停止观察
}
})
})
observer.observe(el)
}
})
进阶方案:结合uni-lazy-load组件(官方组件市场可下载),支持阈值设置和错误处理
// manifest.json配置
"mp-weixin": {
"分包": [
{
"name": "sub-package",
"root": "subPackage",
"pages": ["user/index", "order/list"]
}
],
"subPackages": true
}
效果:主包体积控制在 2MB 以内,分包单包不超过 20MB(微信小程序限制)
{{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
export default {
data() {
return {
username: '',
verificationCode: '',
countdown: 0
}
},
methods: {
async getVerificationCode() {
if (!this.username) return uni.showToast({ title: '请先输入手机号' })
if (this.countdown > 0) return
// 调用验证码接口
await this.$http('/api/sms/code', { phone: this.username }, 'POST')
// 启动倒计时
this.countdown = 60
const timer = setInterval(() => {
this.countdown--
if (this.countdown <= 0) clearInterval(timer)
}, 1000)
},
handleLogin() {
// 调用登录接口并跳转
this.$http('/api/login', {
phone: this.username,
code: this.verificationCode
}).then(res => {
uni.setStorageSync('userInfo', res.data)
uni.navigateTo({ url: '/pages/home/index' })
})
}
}
}
掌握 uniapp 的核心组件特性、跨平台适配策略和性能优化技巧,能够有效提升多端开发效率。建议开发者定期查阅官方更新文档,关注组件市场的优质插件(如 uView、ColorUI)。在 CSDN 发布技术文章时,注重内容结构清晰、案例完整,通过合理的 SEO 优化提升传播效果。
如果需要某部分内容的深度解析(如状态管理集成、第三方 SDK 接入),欢迎在评论区留言,后续将推出专项技术教程。