介绍
抽象定义一个业务对象, 减少前端代码耦合, 数据维护更方便。
推荐更新到最新版本
下载地址
github: https://github.com/2460392754/uniapp-tools/tree/master/model
dcloud: http://ext.dcloud.net.cn/plugin?id=655
更新日志
https://ext.dcloud.net.cn/plugin?id=655&update_log
1.兼容性测试
✅微信小程序
✅h5
✅android
❓其他平台(没测试)
2.目录结构
示例项目目录结构
hBuilderX\plugins\model├── mock 模拟后端数据│ ├── data│ │ └── index.js│ └── index.js├── model model模型生成│ └── index.model.js├── node_modules├── pages│ └── index│ └── index.vue├── plugins 插件│ └── model.js├── static│ └── logo.png├── xhr 数据请求│ └── index.xhr.js├── App.vue├── main.js├── manifest.json├── package-lock.json├── pages.json└── uni.scss
3.如何创建model模型
后端接口的数据定义
- type: 数据类型
- property: 对象的属性路径
- value: 默认值,不填写则以type数据类型设置为默认值, 默认为否值
- 例如: type: String, 则默认值为 “” ```javascript // index.model.js
import Model from ‘../plugins/model’
let model = new Model({ id: { type: Number, property: “uuid”, value: 0 }, name: { type: String, property: “author.nickname”, }, age: { type: Number, property: ‘author.age’, value: 18 }, birthday: { type: String, property: ‘author.birthday’, value: ‘01-01(default)’ }, email: { type: String, property: ‘author.email.qq’, }, });
export default model;
<a name="nbDiX"></a># 4.业务接口文件```javascript// index.xhr.jsimport Model from '../model/index.model'function request (config) {return new Promise((resolve, reject) => {uni.request({...config,success: resolve,fail: reject})})}export default {async getMockData () {const res = await request({ url: '/api/index/mock' })return Model.parse(res.data);}}
5.如何使用
<template><view class="content"><view class='id'>id: {{res.id}}</view><view class='name'>name: {{res.name}}</view><view class='age'>age: {{res.age}}</view><view class='birthday'>birthday: {{res.birthday}}</view><view class='email'>email: {{res.email}}</view></view></template><script>import request from '../../xhr/index.xhr.js'export default {data () {return {res: {},}},methods: {async get () {this.res = await request.getMockData()}},created () {this.get();}}</script><style></style>
最后
出现bug了?
遇到无法解决的问题?
直接加我q或者发邮件也行
email: 2460392754@qq.com
qq: 2460392754
欢迎交流或探讨问题!
