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('/去你心里!')

相关推荐
testleaf8 分钟前
前端面经整理【1】
前端·面试
刘大浪9 分钟前
uniapp 小程序 学习(一)
学习·小程序·uni-app
BillKu9 分钟前
Vue3 + TypeScript + Element Plus 表格行按钮不触发 row-click 事件、不触发勾选行,只执行按钮的 click 事件
vue.js·elementui·typescript
好了来看下一题9 分钟前
使用 React+Vite+Electron 搭建桌面应用
前端·react.js·electron
啃火龙果的兔子10 分钟前
前端八股文-react篇
前端·react.js·前端框架
猎人everest12 分钟前
Django的HelloWorld程序
开发语言·python·django
小前端大牛马16 分钟前
react中hook和高阶组件的选型
前端·javascript·vue.js
刺客-Andy16 分钟前
React第六十二节 Router中 createStaticRouter 的使用详解
前端·javascript·react.js
嵌入式@秋刀鱼1 小时前
《第四章-筋骨淬炼》 C++修炼生涯笔记(基础篇)数组与函数
开发语言·数据结构·c++·笔记·算法·链表·visual studio code
嵌入式@秋刀鱼1 小时前
《第五章-心法进阶》 C++修炼生涯笔记(基础篇)指针与结构体⭐⭐⭐⭐⭐
c语言·开发语言·数据结构·c++·笔记·算法·visual studio code