uniapp将方法挂载到全局

前言

首先需要有一个自己封装的方法,话不多说,直接上代码!

方法文件(common.js)

javascript 复制代码
const getnav = (page, type, param = 'token') => {
	// type 判断是否 需要验证登录
	if (!page) return uni.showModal({
		title: '提示',
		content: '功能暂未开通~',
		showCancel: false
	})
	let user = uni.getStorageSync(param)
	let pages = getCurrentPages();
	console.log(pages)
	if (type) {
		if (!user) {
			// 优化 记录要跳转缓存的页面
			return uni.navigateTo({
				url: "/pages/login/login"
			})
		}
	}
	uni.navigateTo({
		url: page
	})
}
const getback = (delta = 1, date = 500) => {
	setTimeout(() => {
		uni.navigateBack({
			delta,
		});
	}, date)
}
const gettab = (url, date = 0) => {
	setTimeout(() => {
		uni.switchTab({
			url,
		});
	}, date)
}
const getclose = (url, date = 0) => {
	setTimeout(() => {
		uni.redirectTo({
			url,
		});
	}, date)
}
const gettitle = (title, type = 'none', duration = 1500) => {
	uni.showToast({
		title,
		icon: type,
		duration,
	})
}
// 弹框
const getopen = ((title, content, showCancel = true, confirmColor = '#1f73f4', confirmText = '确定', cancelText = '取消',
	cancelColor = '#000000') => {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title,
			content,
			showCancel,
			cancelText,
			cancelColor,
			confirmText,
			confirmColor,
			success: (result) => {
				resolve(result)
			},
			fail: (err) => {
				reject(err)
			},
		});
	})
})
//  自定义 导航头标题
const getnavtitle = (title, ) => {
	uni.setNavigationBarTitle({
		title,
	});
}
// 清除缓存
const romset =(param)=>{
	uni.removeStorageSync(param);
}
// 缓存 
const getset = (param, value) => {
	uni.setStorageSync(param, value);
}
// 获取缓存
const postset = (type = 1,param = 'token') => {
	let token = uni.getStorageSync(param);
	let show;
	if (type == 1) {
		if (token) {
			show = true
		}else {
			show = false
		}
	}
	if(type==2){
		show= token
	}
	return show
}
// 标题颜色
const getColor = (backgroundColor, frontColor = '#ffffff') => {
	uni.setNavigationBarColor({
		frontColor,
		backgroundColor,
		animation: {
			duration: 400,
			timingFunc: 'easeIn'
		}
	})
}
// 关闭当前页 去其他
const getLaunch = (url) => {
	uni.reLaunch({
		url,
	});
}
const getwx = ((data, type) => {
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			provider: 'wxpay',
			timeStamp: data.timeStamp,
			nonceStr: data.nonceStr,
			package: data.package,
			signType: data.signType,
			paySign: data.paySign,
			success: (result) => {
				resolve(result)
			},
			fail: (err) => {
				setTimeout(()=>{
					uni.showToast({
						title: '支付失败!',
						icon: 'none'
					})
				},500)
				if (type == 1) {
					setTimeout(() => {
						this.$uni.getclose('/pagesA/order/order?index=1')
					}, 500)
				} else {
					let obj = {
						errMsg: 'error'
					}
					resolve(obj)
				}
			},
		})
	})
})
export default {
	getnav,
	gettitle,
	getopen,
	getnavtitle,
	getset,
	getback,
	gettab,
	getColor,
	getLaunch,
	getclose,
	getwx,
	postset,
	romset
}

在main.js中挂载

使用方法

this.$uni.gettitle('你是猪猪!')

this.$uni.getLaunch('/去你心里!')

相关推荐
nbsaas-boot14 分钟前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
chao_78918 分钟前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
一斤代码1 小时前
vue3 下载图片(标签内容可转图)
前端·javascript·vue
风无雨1 小时前
GO 启动 简单服务
开发语言·后端·golang
中微子1 小时前
React Router 源码深度剖析解决面试中的深层次问题
前端·react.js
光影少年1 小时前
从前端转go开发的学习路线
前端·学习·golang
斯普信专业组1 小时前
Go语言包管理完全指南:从基础到最佳实践
开发语言·后端·golang
中微子1 小时前
React Router 面试指南:从基础到实战
前端·react.js·前端框架
3Katrina1 小时前
深入理解 useLayoutEffect:解决 UI "闪烁"问题的利器
前端·javascript·面试
前端_学习之路2 小时前
React--Fiber 架构
前端·react.js·架构