首页 > 其他分享 >vue3监听和不能使用this问题,uniapp封装请求

vue3监听和不能使用this问题,uniapp封装请求

时间:2024-10-26 20:18:20浏览次数:3  
标签:uniapp const app vue3 封装 uni import data store

http.js

// 格式化日期函数
export function request(method,url, data) {
    const urls = "http://183.6.96.231:29101";
	const username = uni.getStorageSync('username');
	const token = uni.getStorageSync('token');
   	uni.showLoading({
   		title:'加载中...'
   	});
	const t = Date.parse(new Date()) / 1000;//时间戳
   	return new Promise((resolve, reject) => {
   		uni.request({
   			method: method,
   			url: urls+url+"?t="+t,
   			data: data,
			header: {
				'token': token,
				'user': username,
			},
   			async: false,
   			success(res) {
				uni.hideLoading()
   				resolve(res)
				if(res.data.code == 2006){
					uni.reLaunch({
						url: '/pages/login/login'
					});
					console.log("效验失败,重新登录")
					return true
				}
   			},
   			fail(err) {
   				reject(err);
   				uni.showToast({
   					title: '请求失败',
   					icon: 'none',
   					duration: 1500,
   				})
   				uni.hideLoading()
   			}
   		})
   	})
}

// 导出函数
// export default {request};

main.js

import App from './App'

// #ifndef VUE3
// import Vue from 'vue'
// import './uni.promisify.adaptor'
// Vue.config.productionTip = false
// App.mpType = 'app'

// import store from "./store/index.js"
// Vue.prototype.$store = store
// import {createSSRApp} from 'vue'
// import Vuex from "vuex";

// const app = new Vue({
//   ...App,
//   store
// })
// app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import uviewPlus from '@/uni_modules/uview-plus'
import store from "./store/index.js"
// 全局引入
// import tabbar from '@/components/tabbar.vue';
import {request} from '@/utils/http.js'
export function createApp() {
  const app = createSSRApp(App)
  app.use(store)
  app.use(uviewPlus)
  // app.component('tabbar', tabbar)
  app.config.globalProperties.$http = request
  return {
    app
  }
}
// #endif

1、使用

<template>
	<view style="width: 650rpx;margin-left: 50rpx;margin-top: 30vh;">
		<view class="uni-list-cell-db">
			<u-picker :show="show" :columns="options" @confirm="confirm" @cancel="cancel"></u-picker>
			<view @click="show = true">
				<u-input v-model="form.db" placeholder="套装" border="surround" :readonly="true"></u-input>
			</view>
		</view>
		<view style="margin-top: 20rpx;">
			<u-input v-model="form.user" placeholder="账号" border="surround" clearable></u-input>
		</view>
		<view style="margin-top: 20rpx;">
			<u-input v-model="form.pwd" placeholder="密码" border="surround" clearable password></u-input>
		</view>
		<view style="margin-top: 20rpx;">
			<u-button @click="submit" type="primary" text="确定登录"></u-button>
		</view>
	</view>
</template>

<script setup>
import {ref,reactive,getCurrentInstance,watch} from 'vue';
	//this更会为proxy
	const { proxy } = getCurrentInstance();
	// console.log(proxy.$store.state.tabbar)
	const options = reactive([['wbrj_123']]);
	const show = ref(false)
	const form = reactive({
		'db': 'wbrj_123',
		'user': 'admin',
		'pwd': '123456'
	})
	// 监听对象使用reactive
	watch(form,(newValue,oldValue)=>{
	  console.log(form);
	})
	
	// 监听字符串,可以使用ref
	const dade = ref(0)
	watch(dade,(newValue,oldValue)=>{
	  console.log(`我侦听到了count状态的变化,当前值为${newValue},从而处理相关逻辑`);
	})
	
	// 点击登录
	const submit = ()=>{
		proxy.$http("POST", "/main/admin/login", form.value).then(req => {
			if (req.data.code == 2000) {
				// console.log(req.data.data)
				uni.setStorageSync('username', req.data.data.username);
				uni.setStorageSync('token', req.data.data.token);
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
		})
	}
	const dbClick = ()=>{
		show.value = true
	}
	
	// 点击确定
	const confirm = (e)=>{
		form.value.db = e.value[0]
		show.value = false
	}
	// 点击关闭
	const cancel = (e)=>{
		show.value = false
	}
	
	
</script>


<style scoped lang="scss">
	.u-card-wrap {
		background-color: #000;
		padding: 1px;
	}

	.u-body-item {
		font-size: 32rpx;
		color: #333;
		padding: 20rpx 10rpx;
	}

	.u-body-item image {
		width: 120rpx;
		flex: 0 0 120rpx;
		height: 120rpx;
		border-radius: 8rpx;
		margin-left: 12rpx;
	}
</style>

标签:uniapp,const,app,vue3,封装,uni,import,data,store
From: https://blog.csdn.net/qq_34631220/article/details/143257249

相关文章

  • Vue3取当前时间和一个小时后的时间。
    constformatDate=(date)=>{//初始化时间   //格式化日期为YYYY-MM-DDHH:mm:ss   constyear=date.getFullYear();   constmonth=String(date.getMonth()+1).padStart(2,'0');//月份从0开始   constday=String(date.getDate())......
  • Vue3中取当前日期并且取当前日期的前10天和后30天。并把每个日期的日放到一个数组里面
    consttoday=newDate();//获取当前日期constdateArray=[];constdayArray=[];//计算前10天for(leti=10;i>0;i--){constpastDate=newDate(today);pastDate.setDate(today.getDate()-i);dateArray.push(......
  • 开源一款10k star的聚合支付工具,封装了某宝、某东、某银、PayPal等常用的支付方式的源
    大家好,我是一颗甜苞谷,今天分享一款10kstar的聚合支付工具,封装了某宝、某东、某银、PayPal等常用的支付方式的源码前言在当今的数字化时代,支付已成为日常生活中不可或缺的一部分。然而,面对众多支付渠道,如何高-效、便捷地集成这些支付方式成为一个挑战。为此,我们需要一种处理......
  • KBJ2510-ASEMI整流桥KBJ2510参数、封装、尺寸
    编辑:llKBJ2510-ASEMI整流桥KBJ2510参数、封装、尺寸型号:KBJ2510品牌:ASEMI封装:KBJ-4批号:2024+现货:50000+最大重复峰值反向电压:1000V最大正向平均整流电流(Vdss):25A功率(Pd):大功率芯片个数:4引脚数量:4安装方式:插件类型:插件桥堆、整流桥正向浪涌电流IFSM:350A正向电压:1.......
  • vue3+java+springboot在线考试系统(08169)
    目录功能介绍具体实现截图技术介绍开发核心技术介绍:技术创新点vue3和vue2的区别:核心代码部分展示非功能需求分析系统开发流程软件测试源码获取功能介绍随着社会的发展,系统的管理形势越来越严峻。越来越多的用户利用互联网获得信息,但各种信息鱼龙混杂,信息真假难以辨......
  • GBJ2510-ASEMI整流桥GBJ2510参数、封装、尺寸
    编辑:llGBJ2510-ASEMI整流桥GBJ2510参数、封装、尺寸型号:GBJ2510品牌:ASEMI封装:GBJ-4安装方式:直插批号:2024+现货:50000+正向电流(Id):25A反向耐压(VRRM):1000V正向浪涌电流:350A正向电压(VF):1.10V引脚数量:4芯片个数:4芯片尺寸:MIL功率(Pd):中小功率工作温度:-55°C~150°C类型:整......
  • uniapp精仿支付宝UI界面,首页/理财/消息/生活/口碑/我的,还有模拟支付宝扫码支付/收付款
    uniapp精仿支付宝UI界面,首页/理财/消息/生活/口碑/我的,还有模拟支付宝扫码支付/收付款等功能,界面漂亮颜值高,视频商城小工具等,蚂蚁森林种树养鸡农场偷菜样样齐用于视频,商城,直播,聊天等sumer-alipay介绍uniapp精仿支付宝UI界面,首页/理财/消息/生活/口碑/我的,还有模拟支付宝......
  • uniapp精仿微信源码,基于SumerUI和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视
    uniapp精仿微信源码,基于SumerUI和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视频商城小工具等,朋友圈视频号即时聊天用于视频,商城,直播,聊天,等等场景,源码分享sumer-weixin介绍uniapp精仿微信,基于SumerUI3.0和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视频......
  • Vue3 新特性、Pinia
    一、新特性- defineOptions背景说明因为我们用了<scriptsetup>语法,没办法给setup去提供一些平级的属性,官方就提供了一个叫做defineOptions的语法      所以在Vue3.3中引入了defineOptions宏,用来定义OptionsAPI的选项。可以用defineOptions定义......
  • 基于 Koa + Vue3!一个开源的 Linux 服务器 Web SSH 面板工具!
    大家好,我是Java陈序员。今天,给大家介绍一个Linux服务器WebSSH连接面板工具,基于Koa+Vue3实现!关注微信公众号:【Java陈序员】,获取开源项目分享、AI副业分享、超200本经典计算机电子书籍等。项目介绍EasyNode——一个客户端基于Vue3、服务端基于Koa实现的Linux......