首页 > 其他分享 >uniApp:使用vue3+Vite4+pinia+sass技术栈构建(03)-封装对象类

uniApp:使用vue3+Vite4+pinia+sass技术栈构建(03)-封装对象类

时间:2023-11-07 19:01:21浏览次数:35  
标签:uniApp sass service 03 ResultType user import userModel datas

1.在src文件夹创建models文件夹

uniApp:使用vue3+Vite4+pinia+sass技术栈构建(03)-封装对象类_对象类

import { user } from "@/service/api"
//用户信息返回的数据类型
interface userInfoType {
    username: string,
    phone: string
}
//返回类型
interface ResultType<T> {
    errno: number,
    errmsg: string,
    datas: T
}

class userModel {
    datas: ResultType<userInfoType> = {
        errno: 0, errmsg: "", datas: {
            username: "",
            phone: ""
        }
    }
    async getInfo() {
        uni.showLoading({
            title: "加载中",
        })
        let redatas = await user.getUserInfo<userInfoType>()
        if (redatas.errno == 0) {
            this.datas = redatas
        }
        uni.hideLoading()
    }

}

export { userModel }
export type { ResultType, userInfoType }

2.修改src/service/module/user.ts,使其改成ts写法

import type { ResultType } from "@/models/userModel"
import service from "../service"
const user = {
    getUserInfo<T>(): Promise<ResultType<T>> {
        return service.post<Promise<ResultType<T>>>("/api/getUserInfo", {})
    }
}
export default user

3.在页面中使用

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png" />
		<view class="text-area">
			<text class="title">{{ title }}-{{ userObj.datas.datas.username }}</text>
		</view>
	</view>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue"
import { userModel } from "@/models/userModel"
const userObj = reactive(new userModel())
userObj.getInfo()

const title = ref("Hello")
</script>


标签:uniApp,sass,service,03,ResultType,user,import,userModel,datas
From: https://blog.51cto.com/u_3692993/8238541

相关文章

  • uniapp之文件保存
    uniapp之文件保存文件保存分几种情况:1.网络文件保存:使用uni.downloadFile创建临时文件地址,然后使用uni.saveFile保存uni.downloadFile({//下载 url:path, success:(res)=>{ if(res.statusCode==200){ uni.saveFile({ tempFileP......
  • 正则表达式-r'\[.*\]'和r'[.*]'的区别
    则表达式r'[.*]'实际上会匹配包含任何一个字符*或.或*的字符串。这不是想要的结果。希望匹配方括号[]中的任意字符,并且要匹配多个字符。要匹配方括号[]中的任意字符,并匹配多个字符,你应该使用r'\[.*\]',其中\[匹配左方括号,.*匹配任意字符(包括数字、字母等等),然后\]......
  • uniapp做移动端手写签名生成图片
    1、电子签名电子签名目前已经有很多现成的插件,所以我这里直接选择了一个,有需要的可以看下https://ext.dcloud.net.cn/plugin?id=4354代码如下:HTML:<viewclass="content"> <viewstyle="width:750rpx;height:400px;"> <l-signaturedisableScrollbackgroundColor="......
  • China's Strategies on Air Pollution
    IntroductionAveragedpercentagechangesinPM2.5annualconcentrationsin31majorChinesecitiessince2015.Credit:ScienceChinaPressAnewstudyhasbeenconductedtounderstandwhethertherecentchangesinChina'sairqualityweredriven......
  • win10定时关机、定时休眠和定时睡眠的命令行【转载自https://blog.csdn.net/conanluff
    1、定时关机#注:/s关机,/t后面接时间,单位是秒shutdown/s/t3600 #一个小时后关机shutdown-a#取消定时关机注:"shutdown/h"是立即进入休眠的命令,但/h参数不能与/t连用,故不能用shutdown实现定时休眠的功能。/h可与/f连用,/f表示强制关闭正在运行的应用程序而不事先警告用户。......
  • Windows XP/2003自动登录
    单击“开始|运行”,并在输入框中键入“controluserpasswords2”,这样就可以在“用户账户”管理窗口中清除“要使用本机,用户必须输入密码”复选项的选中状态,然后按下键盘的“Ctrl+Shift+A”,将会得到一个“自动登录”的设置对话框,您可以按自己的需要设置系统在电脑启动时自动登录使用......
  • uniapp实用功能代码(小程序支付,图片保存,返回刷新,分享到朋友圈)
    1.uniapp小程序支付:uni.request({url:"http://xxxxxx/payOrder",//后端接口返回调起支付需要的参数data:{userId:1,//此接口需要的参数一般有多个此仅为示例},method:"POST",success:(res)=>{console.log(res.data,"这......
  • 如何在 Word 2003 中恢复丢失的文件
    本文向您介绍可用于恢复丢失的文档的一些步骤。关于:打不开Word文件。搜索原文档单击“开始”,然后单击“搜索”。单击MicrosoftWindows资源管理器左侧的“搜索助理”中的“所有文件和文件夹”。在“全部或部分文件名:”框中,键入要查找的文件名。在“在这里寻找”框中,单击“我......
  • SQL Server,Could not obtain exclusive lock on database 'model'
    创建SQLServer数据库时出现错误“Couldnotobtainexclusivelockondatabase'model'”尝试以下方法:1.totryreconnectingtothedatabase.2.Restartingtheservice.3.killingthespidholdingthelock. 执行以下SQL语句来查询:select d.name,resource_type,resour......
  • 怎样取消Windows 2003 server 意外关机提示
    怎样取消Windows2003server意外关机提示取消系统关机原因方法:(1)打开“开始”--“运行”命令,打开一个运行对话框,在其中输入“gpedit.msc”命令,来弹出组策略编辑界面。按回车后打开组策略编辑器。(2)在该界面中,用鼠标左键双击“计算机配置”文件夹----“管理模板”文件夹----“......