uniapp代码片段
应用过的代码片段集,不定期更新
设置当前页面title
// 设置当前页面title
uni.setNavigationBarTitle({
title: user.name
})
跳转页面
// 保留当前页面跳转指定页面 注意:新加页面跳转不成功留意pages配置
uni.navigateTo({
url: `/pages/index/chat/chat?id=${id}`,
animationType: 'slide-in-right',
animationDuration: 300,
})
让输入框随键盘动
<!-- 让输入框随键盘动 -->
<u--textarea
:cursor-spacing="20" // 核心
v-model="value"
placeholder="请输入内容"
autoHeight
></u--textarea>
<!--父组件自定义@getEmoji事件 -->
<emoji-box v-if="showEmoji" @getEmoji="getEmoji">
</emoji-box>
<!-- 子组件触发 this.$emit('getEmoji',emoji) -->
监听键盘高度变化
// 监听键盘高度变化
uni.onKeyboardHeightChange((res) => {
if (res.height === 0) {
if (that.showEmoji) {
that.showEmoji = false;
}
}
})
弹出框
// 弹出框
uni.showToast({
title:'登陆中',
icon:'loading',
duration: 3000
})
等待框
// 开启等待框
uni.showLoading({
title: '等待',
mask: true,
});
// 结束等待框
uni.hideLoading();
同步存储数据
// 同步存储数据
uni.setStorageSync('token', token);
// 同步清除数据
uni.clearStorageSync('userInfo')
跳转浏览器
// 跳转浏览器
plus.runtime.openURL(url)
// 人为交互调用需要小括号 @click="submitBind()"
// 自定义事件不能带小括号 @confirm="confirm"
复制
// 复制
uni.setClipboardData({
data: this.formData.email,
success() {
uni.hideToast(); // 组织复制成功提示
}
})
请求
const BASE_URL = 'http://192.168.2.23:4100';
// options: {url: '/login', method: 'get',data: {}}
export default function(options) {
return new Promise((resolve, reject) => {
uni.request({
url: `${BASE_URL}${options.url}`,
method: `${options.method || 'GET'}`,
data: options.data,
header: {
'content-type': 'application/x-www-form-urlencoded;charset:utf-8'
},
success(res) {
// 这里做响应拦截
resolve(res)
},
fail(err) {
// 请求失败了
reject(err)
},
})
})
}
获取当前路由
const pages = getCurrentPages();
pages[pages.length - 1] // 当前路由
uniapp的iframe
<web-view :src="Url"></web-view>
获取经纬度
uni.getLocation({
type: 'wgs84',
success: function (res) { b
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
}
});