在utils目录下添加network.js
文件
// 监听网络变化
export const watchNetwork = () => {
// #ifdef APP
uni.onNetworkStatusChange((res) => {
if (res.networkType && res.networkType !== "none") {
uni.showModal({
title: "温馨提示",
content: "已连接网络,请重新加载应用",
showCancel: false,
success: (res) => {
if (res.confirm) {
plus.runtime.restart();
}
}
})
}
})
// #endif
}
// 获取当前网络状态
export const getNetworkType = () => {
// #ifdef APP
uni.getNetworkType({
success: (res) => {
if (res.networkType && res.networkType === "none") {
uni.showToast({
title: "当前未开启网络,请前往设置开启",
icon: "none"
})
watchNetwork();
}
}
})
// #endif
}
在App.vue
添加以下代码
import { getNetworkType } from "@/utils/network.js";
export default {
onLaunch: function() {
getNetworkType();
}
}
0 个评论
要回复文章请先登录或注册