


When developing WeChat mini programs, why do I only use upData?
In view of the poor experience of using setData
when developing WeChat applet, I developed a library function wx-updata
. After the project was launched, , I compiled this self-used library function and put it on Github as open source wx-updata. This library function was very helpful to me during development. I hope it can also help everyone. If you encounter any problems during use, If you have a problem, you can submit a PR or an issue to me, and let’s work together to improve the mini program development experience~
- wx-updata
Version 0.0.10
Github address: github.com/SHERlocked9… - Mini program code snippet preview address: developers.weixin.qq.com/s/CcXdO1mc7…
- Mini program code snippet code address: github.com/SHERlocked9…
- 1. The inconvenient place of setData
You are using Do you sometimes feel uncomfortable when
setData? Here is a simple example: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>// 你的 datadata: { name: &#39;蜡笔小新&#39;, info: { height: 140, color: &#39;黄色&#39; }
}复制代码</pre><div class="contentsignin">Copy after login</div></div>
If you want to modify
to 155, use How to do setData
: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>// 这样会把 info 里其他属性整不见了this.setData({ info: { height: 155 } })// 你需要取出 info 对象,修改后整个 setDataconst { info } = this.data
info.height = 155this.setData({ info })复制代码</pre><div class="contentsignin">Copy after login</div></div>
does not seem too complicated, but if
is a large object, deep and different objects and array items must be changed one by one. : <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>data: { name: &#39;蜡笔小新&#39;, info: { height: 140, color: &#39;黄色&#39;, desc: [{ age: 8 }, &#39;最喜欢大象之歌&#39;, &#39;靓仔&#39;, { dog: &#39;小白&#39;, color: &#39;白色&#39; }]
}
}复制代码</pre><div class="contentsignin">Copy after login</div></div>
For example, for a certain requirement, you need to change
to 155, and at the same time change the age# of the 0th item of the
info.desc array. ## is 12, and the
color of the third item is gray?
// 先取出要改变的对象,改变数字后 setData 回去const { info } = this.data info.height = 155info.desc[0].age = 12info.desc[3].color = '灰色'this.setData({ info })// 或者像某些文章里介绍的,这样可读性差,也不太实用this.setData({ 'info.height': 155, 'info.desc[0].age': 12, 'info.desc[3].color': '灰色'})复制代码
The above two methods are often used in our daily small programs. Compared with other web-side frameworks, they are very lame and have a strong sense of semi-finished products. Is there such a one? Method:
this.upData({ info: { height: 155, desc: [{ age: 12 }, , , { color: '灰色' }] } })复制代码
setData
The principle of wx-updata is actually very simple, for example:
this.upData({ info: { height: 155, desc: [{ age: 12 }] } })// 会被自动转化为下面这种格式,// this.setData({// 'info.height': 155,// 'info.desc[0].age': 12,// })复制代码
Supports
setData- objects Automatic merging, no need to write crappy object paths
Supports nested arrays within objects, and nested objects within arrays;
If you don’t want to overwrite a certain value in the array, please use the array space to skip this array item, such as - [1,,3] The middle of this array is the array empty space;
If your
Eslint reports an error if the array is empty, you can use - wx-updata
Provide Empty instead:
[1, Empty, 3]If you are not used to empty spaces in the array, or are not willing to count commas, you can try Try the object path method of the array
[1,,3] -> - {'[0]': 1, '[2]': 3}
You can also directly copy the
wx-updata.jsin the,dist
npmdirectory to the project for use
Use
yarn Installation method:
$ npm i -S wx-updata# or$ yarn add wx-updata复制代码
Then:
Install the WeChat developer tools
Details on the right side of the panel - Local Settings - Use the npm module- button to open;
Click the
Tools on the WeChat Developer Tool Panel toolbar - Build npm ; -
After the build, the
miniprogram_npm
4. How to use wx-updata
Page
, so that you can useupData in the
Page instance just like
setData
// app.jsimport { updataInit } from './miniprogram_npm/wx-updata/index' // 你的库文件路径App({ onLaunch() { Page = updataInit(Page, { debug: true }) } })复制代码
// 页面代码中this.upData({ info: { height: 155 }, desc: [{ age: 13 }, '帅哥'], family: [, , [, , , { color: '灰色' }]] })复制代码
Usage method two
Some frameworks may have further modifications on the Page
object, and direct replacement ofPage may not be possible. Great,
wx-updata also exposes tool methods. Users can use tool methods directly in the page code for processing:
// 页面代码中import { objToPath } from './miniprogram_npm/wx-updata/index' // 你的库文件路径Page({ data: { a: { b: 2}, c: [3,4,5]}, // 自己封装一下 upData(data) { return this.setData(objToPath(data)) }, // 你的方法中或生命周期函数 yourMethod() { this.upData({ a: { b: 7}, c: [8,,9]}) } })复制代码
Use Empty to replace array empty spaces
You can use the Empty provided by wx-updata
to replace the array empty position. Since Empty is essentially a Symbol, it can only be exported usingwx-updata and cannot be created by yourself.
// 页面代码中import { Empty } from './miniprogram_npm/wx-updata/index'this.upData({ info: { height: 155 }, desc: [{ age: 13 }, '帅哥'], family: [Empty, Empty, [Empty, Empty, Empty, { color: '灰色' }]] })复制代码
Object path method of array
If you are not used to empty spaces in the array, or are not willing to count commas, you can try the object path method of array, and you need to pass the configuration of config{arrObjPath: true}
// 页面代码中import { Empty } from './miniprogram_npm/wx-updata/index'// 原来的方式this.upData({ info: { height: 155 }, desc: [, '靓仔'], family: [, , [, , , { color: '灰色' }]] })// 使用数组路径方式this.upData({ info: { height: 155 }, desc: {'[1]': '靓仔'}, family: { '[2]': { '[3]': { color: '灰色' } })复制代码
5. wx-updata related API
Page.prototype.upData(Object data, Function callback)
-
data
: The data you want to set -
callback
: Same as the second parameter of setData, it causes the callback function after the interface is updated and rendered.
updataInit(Page, config)
- ##Page
: Page object, needs to be in
app.jsCalled in;
- config
Configuration
- Configuration parameters
- { debug: true }
will print out the pathed data, help For user debugging, the default value is false and is not enabled;
Configuration parameters - { arrObjPath: true }
will enable the object path mode function of the array. The default value is false and is not enabled;
- { debug: true }
objToPath(Object data, Object config)
- data
: The data object you want to set
- config
Configuration
- Configuration parameters
- { arrObjPath: true }
, will enable the object path mode function of the array, the default is false and will not be enabled;
- { arrObjPath: true }
More related free learning recommendations: WeChat Mini Program Development
The above is the detailed content of When developing WeChat mini programs, why do I only use upData?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the <swiper> tag to achieve the switching effect of the carousel. In this component, you can pass b

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to

Implementing the sliding delete function in WeChat mini programs requires specific code examples. With the popularity of WeChat mini programs, developers often encounter problems in implementing some common functions during the development process. Among them, the sliding delete function is a common and commonly used functional requirement. This article will introduce in detail how to implement the sliding delete function in the WeChat applet and give specific code examples. 1. Requirements analysis In the WeChat mini program, the implementation of the sliding deletion function involves the following points: List display: To display a list that can be slid and deleted, each list item needs to include
