Table of Contents
You are using Do you sometimes feel uncomfortable when
Supports
You can also directly copy the
Page
wx-updata
{arrObjPath: true}
Page.prototype.upData(Object data, Function callback)
Home WeChat Applet Mini Program Development When developing WeChat mini programs, why do I only use upData?

When developing WeChat mini programs, why do I only use upData?

Sep 27, 2020 pm 04:45 PM
WeChat applet

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: &amp;#39;蜡笔小新&amp;#39;, info: { height: 140, color: &amp;#39;黄色&amp;#39; } }复制代码</pre><div class="contentsignin">Copy after login</div></div> If you want to modify

info.height

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

data

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: &amp;#39;蜡笔小新&amp;#39;, info: { height: 140, color: &amp;#39;黄色&amp;#39;, desc: [{ age: 8 }, &amp;#39;最喜欢大象之歌&amp;#39;, &amp;#39;靓仔&amp;#39;, { dog: &amp;#39;小白&amp;#39;, color: &amp;#39;白色&amp;#39; }] } }复制代码</pre><div class="contentsignin">Copy after login</div></div>For example, for a certain requirement, you need to change

info.height

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 = &#39;灰色&#39;this.setData({ info })// 或者像某些文章里介绍的,这样可读性差,也不太实用this.setData({    &#39;info.height&#39;: 155,    &#39;info.desc[0].age&#39;: 12,    &#39;info.desc[3].color&#39;: &#39;灰色&#39;})复制代码
Copy after login
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: &#39;灰色&#39; }]
    }
})复制代码
Copy after login

This method will help us deeply change the corresponding attribute values ​​​​in the nested object, skip the array items that we do not want to change, and only set the attribute values ​​​​and array items we provided. Isn’t it omitted? A lot of crappy code that's extremely readable.

This is why I use wx-updata in the online project instead of

setData

The principle of wx-updata is actually very simple, for example:

this.upData({    info: {        height: 155,        desc: [{ age: 12 }]
    }
})// 会被自动转化为下面这种格式,// this.setData({//    &#39;info.height&#39;: 155,//    &#39;info.desc[0].age&#39;: 12,// })复制代码
Copy after login

Originally we had to do this conversion manually ourselves, but now wx-updata does it for us, isn’t it wonderful!

The following introduces the advantages and main usage methods of wx-updata~

2. Advantages of wx-updata

Supports

setData
    objects Automatic merging, no need to write crappy object paths
  1. Supports nested arrays within objects, and nested objects within arrays;
  2. 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
  3. [1,,3]
  4. The middle of this array is the array empty space;
  5. If your Eslint
  6. reports an error if the array is empty, you can use
  7. 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]
  8. ->
  9. {'[0]': 1, '[2]': 3} 3. wx-updata installation

You can also directly copy the

wx-updata.js
in the

dist directory to the project for useUse

npm
,

yarn Installation method:

$ npm i -S wx-updata# or$ yarn add wx-updata复制代码
Copy after login
Then:

Install the WeChat developer tools

Details on the right side of the panel - Local Settings - Use the npm module
    button to open;
  1. Click the Tools on the WeChat Developer Tool Panel toolbar - Build npm
  2. ;
  3. After the build, the
  4. miniprogram_npm
folder is successfully generated and can be used normally.

4. How to use wx-updata

Usage method one

You can use the method of mounting directly to

Page

, so that you can use

upData in the Page instance just like setData

// app.jsimport { updataInit } from &#39;./miniprogram_npm/wx-updata/index&#39;  // 你的库文件路径App({
    onLaunch() {
        Page = updataInit(Page, { debug: true })
    }
})复制代码
Copy after login
// 页面代码中this.upData({    info: { height: 155 },    desc: [{ age: 13 }, &#39;帅哥&#39;],    family: [, , [, , , { color: &#39;灰色&#39; }]]
})复制代码
Copy after login
Usage method two

Some frameworks may have further modifications on the

Page

object, and direct replacement of

Page 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 &#39;./miniprogram_npm/wx-updata/index&#39;  // 你的库文件路径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]})
    }
})复制代码
Copy after login
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 using

wx-updata and cannot be created by yourself.

// 页面代码中import { Empty } from &#39;./miniprogram_npm/wx-updata/index&#39;this.upData({    info: { height: 155 },    desc: [{ age: 13 }, &#39;帅哥&#39;],    family: [Empty, Empty, [Empty, Empty, Empty, { color: &#39;灰色&#39; }]]
})复制代码
Copy after login
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 &#39;./miniprogram_npm/wx-updata/index&#39;// 原来的方式this.upData({    info: { height: 155 },    desc: [, &#39;靓仔&#39;],    family: [, , [, , , { color: &#39;灰色&#39; }]]
})// 使用数组路径方式this.upData({    info: { height: 155 },    desc: {&#39;[1]&#39;: &#39;靓仔&#39;},    family: { &#39;[2]&#39;: { &#39;[3]&#39;: { color: &#39;灰色&#39; }
})复制代码
Copy after login
5. wx-updata related API

Page.prototype.upData(Object data, Function callback)

  1. data: The data you want to set
  2. callback: Same as the second parameter of setData, it causes the callback function after the interface is updated and rendered.

updataInit(Page, config)

  1. ##Page: Page object, needs to be in app.js Called in;
  2. 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;

objToPath(Object data, Object config)

  1. data: The data object you want to set
  2. 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;

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Xianyu WeChat mini program officially launched Xianyu WeChat mini program officially launched Feb 10, 2024 pm 10:39 PM

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;

Implement image filter effects in WeChat mini programs Implement image filter effects in WeChat mini programs Nov 21, 2023 pm 06:22 PM

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

Implement the drop-down menu effect in WeChat applet Implement the drop-down menu effect in WeChat applet Nov 21, 2023 pm 03:03 PM

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 image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

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

What is the name of Xianyu WeChat applet? What is the name of Xianyu WeChat applet? Feb 27, 2024 pm 01:11 PM

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 WeChat applet to achieve carousel switching effect Use WeChat applet to achieve carousel switching effect Nov 21, 2023 pm 05:59 PM

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 &lt;swiper&gt; tag to achieve the switching effect of the carousel. In this component, you can pass b

Implement image rotation effect in WeChat applet Implement image rotation effect in WeChat applet Nov 21, 2023 am 08:26 AM

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

Implement the sliding delete function in WeChat mini program Implement the sliding delete function in WeChat mini program Nov 21, 2023 pm 06:22 PM

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

See all articles