全局变量:
https://ask.dcloud.net.cn/article/35021
定义一个专用的模块,用来组织和管理这些全局的变量,在需要的页面引入。
注意这种方式只支持多个vue页面或多个nvue页面之间公用,vue和nvue之间不公用。
App.vue
<script>
var serialport = uni.requireNativePlugin("wrs-serialport")
var bluetoothcenter = uni.requireNativePlugin("wrs-bluetoothcenter")
export default {
globalData: {
serialport: serialport,
bluetoothcenter: bluetoothcenter
},
onLaunch: function() {
console.log('App Launch')
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script>
<style>
/*每个页面公共css */
</style>
使用:
data() {
let bluetoothcenter = getApp().globalData.bluetoothcenter
return {
bluetoothcenter: bluetoothcenter
}
},
msg自动换行
<text class="msg">{{msg}}</text>
.msg {
white-space: normal; /* 或者 pre-wrap,根据需求选择 */
word-break: break-all;
margin-left: 20upx;
margin-right: 20upx;
}
判断是iOS还是Android
var isAndroid = true
switch (uni.getSystemInfoSync().platform) {
// android
case 'android':
isAndroid = true
break;
// ios
default:
isAndroid = false
break;
}
相对路径与绝对路径转换
var url="_www/index.html";
var path=plus.io.convertLocalFileSystemURL(url);
var newurl = plus.io.convertAbsoluteFileSystem(path);
Android请求动态权限
this.requestPermission([
"android.permission.BLUETOOTH_ADMIN",
"android.permission.BLUETOOTH",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
])
requestPermission: function(permissions) {
plus.android.requestPermissions(
permissions,
function(resultObj) {
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i];
console.log('已获取的权限:' + grantedPermission);
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i];
console.log('拒绝本次申请的权限:' + deniedPresentPermission);
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i];
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
}
// 若所需权限被永久拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
if (resultObj.deniedAlways.length > 0) {
// var Intent = plus.android.importClass("android.content.Intent");
// var Settings = plus.android.importClass("android.provider.Settings");
// var Uri = plus.android.importClass("android.net.Uri");
// var mainActivity = plus.android.runtimeMainActivity();
// var intent = new Intent();
// intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
// var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
// intent.setData(uri);
// mainActivity.startActivity(intent);
}
},
function(error) {
console.log('申请权限错误:' + error.code + " = " + error.message);
});
},
showModel: function(msg) {
uni.showModal({
title: '提示',
content: msg,
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
},
showToast: function(msg) {
uni.showToast({
title: msg,
duration: 2000
});
},
showActionSheet: function(itemList, clickFun) {
uni.showActionSheet({
itemList: itemList,
success: function(res) {
if (clickFun) {
clickFun(res.tapIndex);
}
},
fail: function(res) {
console.log(res.errMsg);
}
});
},
clear() {
this.msg = null
},
showMsg(info) {
console.log(info)
if (this.msg) {
this.msg = this.msg + "\n" + info
} else {
this.msg = info
}
}
全局变量:
main.js
var wrsSqlite = uni.requireNativePlugin("wrs-sqlite");
Vue.prototype.$wrsSqlite = wrsSqlite
使用:
this.$wrsSqlite.setDBCallback((resp) => {
var opt = resp.opt;
if (opt == "onCreate") {
this.showMsg("创建数据库");
} else if (opt == "onOpen") {
this.showMsg("打开数据库");
this.initTable();
} else if (opt == "onUpgrade") {
var oldVersion = resp.oldVersion;
var newVersion = resp.newVersion;
this.showMsg("数据库升级,oldVersion:" + oldVersion + " newVersion:" + newVersion);
}
});
user.js
var wrsSqlite = uni.requireNativePlugin("wrs-sqlite");
export function findAllUser() {
wrsSqlite.findAllUser()
}
原生与uniapp的长度单位关系
const {
windowWidth,
windowHeight,
statusBarHeight
} = uni.getSystemInfoSync();
console.log("UNIApp windowWidth:" + windowWidth + " windowHeight:" + windowHeight)
let info = getScreenInfo()
console.log("原生:" + JSON.stringify(info))
iphone X:
windowWidth:375 windowHeight:724
原生:{"height":812,"scale":3,"width":375}
812 = 724 + 44(状态栏) + 34
android:
UNIApp windowWidth:362 windowHeight:714
原生:{"widthPixels":1080,"density":2.987499952316284,"heightPixels":2265,"scaledDensity":2.987499952316284,"densityDpi":478}
1080 = 362 * 2.987499952316284(density)
结论:
uni.getSystemInfoSync() 获取的回来的数值单位是dp