【微信小程序】使用npm包实现API Promise化和全局数据共享

1.使用npm包

小程序对 npm 的支持与限制

目前,小程序中已经支持使用 npm 安装第三方包,从而来提高小程序的开发效率。但是,在小程序中使用

npm 包有如下 3 个限制:

① 不支持依赖于 Node.js 内置库的包

② 不支持依赖于浏览器内置对象的包

③ 不支持依赖于 C++ 插件的包

总结:虽然 npm 上的包有千千万,但是能供小程序使用的包却“为数不多”。

1.Vant Weapp

1. 什么是 Vant Weapp

Vant Weapp 是有赞前端团队开源的一套小程序 UI 组件库,助力开发者快速搭建小程序应用。它所使用的是

MIT 开源许可协议,对商业使用比较友好。

官方文档地址 https://youzan.github.io/vant-weapp

2. 安装 Vant 组件库

在小程序项目中,安装 Vant 组件库主要分为如下 3 步:

① 通过 npm 安装(建议指定版本为@1.3.3)

② 构建 npm 包

③ 修改 app.json

详细的操作步骤,大家可以参考 Vant 官方提供的快速上手教程:

https://youzan.github.io/vant-weapp/#/quickstart#an-zhuang

3. 使用 Vant 组件

安装完 Vant 组件库之后,可以在 app.json 的 usingComponents 节点中引入需要的组件,即可在 wxml 中

直接使用组件。示例代码如下:

image-20220823112358918

4. 定制全局主题样式

Vant Weapp 使用 CSS 变量来实现定制主题。 关于 CSS 变量的基本用法,请参考 MDN 文档:

https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties

在 app.wxss 中,写入 CSS 变量,即可对全局生效:

image-20220823121011187

所有可用的颜色变量,请参考 Vant 官方提供的配置文件:

https://github.com/youzan/vant-weapp/blob/dev/packages/common/style/var.less

2.API Promise化

1 . 基于回调函数的异步 API 的缺点

默认情况下,小程序官方提供的异步 API 都是基于回调函数实现的,例如,网络请求的 API 需要按照如下的方 式调用:

image-20220823212958905

缺点:容易造成回调地狱的问题,代码的可读性、维护性差!

2.什么是 API Promise 化

API Promise化,指的是通过额外的配置,将官方提供的、基于回调函数的异步 API,升级改造为基于 Promise 的异步 API,从而提高代码的可读性、维护性,避免回调地狱的问题。

3. 实现 API Promise 化

在小程序中,实现 API Promise 化主要依赖于 miniprogram-api-promise 这个第三方的 npm 包。它的安装 和使用步骤如下:

npm install --save [email protected]

image-20220823213446659

4.调用 Promise 化之后的异步 API

image-20220823215720469

3.全局数据共享

1 . 什么是全局数据共享

全局数据共享(又叫做:状态管理)是为了解决组件之间数据共享的问题。 开发中常用的全局数据共享方案有: Vuex、 Redux、 MobX 等。

image-20220823215938195

  1. 小程序中的全局数据共享方案

在小程序中,可使用 mobx-miniprogram 配合 mobx-miniprogram-bindings 实现全局数据共享。其中:

⚫ mobx-miniprogram 用来创建 Store 实例对象

⚫ mobx-miniprogram-bindings 用来把 Store 中的共享数据或方法,绑定到组件或页面中使用

image-20220823220644508

1 . 安装 MobX 相关的包

在项目中运行如下的命令,安装 MobX 相关的包:

npm i --save [email protected] [email protected]

注意: MobX 相关的包安装完毕之后,记得删除 miniprogram_npm 目录后,重新构建 npm。

2.创建 MobX 的 Store 实例

image-20220823221328132

store/store.js
import {
    
    action, observable} from "mobx-miniprogram"

export const store=observable({
    
    
    //数据字段
    numA:1,
    numB:2,
    //计算属性
    get sum(){
    
    
        return this.numA+this.numB
    },
    //actions:用来修改store数据
    updateNum1:action(function(step){
    
    
        this.numA=step
    }),
    updateNum2:action(function(step){
    
    
        this.numB=step
    })
})

3.将 Store 中的成员绑定到页面中

image-20220823222606554

import {
    
    createStoreBindings} from "mobx-miniprogram-bindings"
import {
    
    store} from "../../store/store"

  onLoad() {
    
    
   this.storebindings= createStoreBindings(this,{
    
    
        store,
        fields:['numA','numB','sum'],
        actions:['updateNum1']
    })
  },
  
  onUnload(){
    
    
      this.storebindings.destroyStoreBindings()
  }
  1. 在页面上使用 Store 中的成员

image-20220823225026925

<!--logs.wxml-->
<view>{
   
   {numA}}+{
   
   {numB}}={
   
   {sum}}</view>
<vant-button type="primary" bindtap="btnhander" data-step="{
     
     {1}}">numA +1</vant-button> 
<vant-button type="danger" bindtap="btnhander" data-step="{
     
     {-1}}">numA -1</vant-button> 
// logs.js
import {
    
    createStoreBindings} from "mobx-miniprogram-bindings"
import {
    
    store} from "../../store/store"

Page({
    
    
  data: {
    
    
    logs: []
  },
  onLoad() {
    
    
   this.storebindings= createStoreBindings(this,{
    
    
        store,
        fields:['numA','numB','sum'],
        actions:['updateNum1']
    })
  },
  btnhander(e){
    
    
    //console.log(e);
    this.updateNum1(e.target.dataset.step)
  },
  onUnload(){
    
    
      this.storebindings.destroyStoreBindings()
  }
})

image-20220823225113067

  1. 将 Store 中的成员绑定到组件中

image-20220823225204427

<!--components/number/number.wxml-->
<view>{
   
   {numA}}+{
   
   {numB}}={
   
   {sum}}</view>
<vant-button type="primary" bindtap="btnhander2" data-step="{
     
     {1}}">numB +1</vant-button> 
<vant-button type="danger" bindtap="btnhander2" data-step="{
     
     {-1}}">numB -1</vant-button>

// components/number/number.js
import {
    
    storeBindingsBehavior} from "mobx-miniprogram-bindings"
import {
    
    store} from "../../store/store"
Component({
    
    
    behaviors:[storeBindingsBehavior],
    storeBindings:{
    
    
        store,
        fields:{
    
    
            numA:'numA',
            numB:'numB',
            sum:'sum'
        },
        actions:{
    
    
            updateNum2:'updateNum2'
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
    
    
        btnhander2(e){
    
    
            this.updateNum2(e.target.dataset.step)
        }
    }
})

image-20220823230740694

image-20220824132414625

猜你喜欢

转载自blog.csdn.net/m0_46615524/article/details/126637542