当前位置: 首页 > news >正文

微信131~140

1.在组件中使用store对象的数据
// 要想使用store中的数据以及方法
// 需要从 mobx-miniprogram-bindings 方法将 ComponentWithStore 方法
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
// 导入store对象
import { numStore } from '../../../stores/numstore'
// 需要使用 ComponentWithStore 方法将 Component 方法替换
ComponentWithStore({data: {},methods: {},//配置当前组件需要与那些store进行关联// 数据会被引到data对象中 // 方法会被注入到methods对象中storeBindings: {// 引入的是那个storestore: numStore,// 引入的是那些数据fields: ['numA', 'numB', 'sum'],// 用的 方法action: ['update']}
})
2.页面中使用store对象----方法一
// 要全局注册
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
import { numStore } from '../../../store/numstore'
//小程序页面也可以用Component 进行构造
// 如果页面中张使用store对象中的数据,使用方式和组件的使用方式一模一样
ComponentWithStore({data: {},methods: {},// 数据会被引到data对象中 // 方法会被注入到methods对象中storeBindings: {store: numStore,fields: ['numA', 'numB', 'sum'],action: ['update']}
})
3.页面中使用store对象----方法二

可以用 mobx-miniprogram-bindings 提供的BehaviorWithStore方法和store建立关联

import { BehaviorWithStore } from 'mobx-miniprogram-bindings'
import { numstore } from '../../../store/numstore'
export const cartBehavior = BehaviorWithStore({storeBindings: {store: numstore,fields: ['numA', 'numB', 'sum'],action: ['update']}
})import { cartBehavior } from './behavior'
Page({//使用behaviors配置项注册提取的bahaviorbehaviors: [cartBehavior]
})
4.fields、actions对象写法

首推数组写法
在这里插入图片描述
之前是数组写法
现在是对象写法
fields:
映射形式:需要指定data中那些字段来源于store,以及在store中的名字是什么

numA: 'numA',
numB: 'numB',
sum: 'sum'

函数形式: key:data中那些字段来源于store;value:函数及函数内部需要返回对应store数据的值。

numA: () => numstore.numA,
numB: () => numstore.numB,
sum: () => numstore.sum,

自定义形式:如果对属性进行了自定义,模板中需要使用自定义以后的属性。
actions:
映射形式:指定模板中那些方法来源于store,以及在store中的名字是什么

updateData: ' update '
5.绑定多个store以及命名空间

一个页面或组件可能会绑定多个Store,我们可以将storeBindings 改造成数组。数组每一项就是一个个要绑定的 Store 。

如果多个 Store 中存在相同的数据,显示会出现异常,还可以通过 namespace 属性给当前 Store 开启命名空间,在开启命名空间以后,访问数据的时候,需要加上 namespace 的名字才可以。

{store: 'numstore',fields: ['numA', 'numB', 'sum'],action: {updateData: 'update'}}
6.miniprogram-computed计算属性和监听器

如果需要在组件中使用计算属性功能,需要 miniprogram-computed 库中导入 ComponentWithComputed 方法

在使用时:需要将 Component 方法替换成 ComponentWithComputed 方法 ,原本组件配置项也需要写到该方法中

在替换以后,就可以新增 computed 以及 watch 配置项。

计算属性方法内部必须有返回值。
在计算属性中,不能使用this来获取data中的函数,如果想要获取,就使用形参。
计算属性具有缓存特性,它只执行一次,后续在使用的时候,返回的是第一次执行的结果,只要依赖的数据没有发生改变,返回的始终是第一次执行的结果。

import { ComponentWithComputed } from 'miniprogram-computed'Component({computed: {total (data) {// 计算属性方法内部必须有返回值//在计算属性中,不能使用this来获取data中的函数,如果想要获取,就使用形参return data.a + data.b}},data: {a: 1,b: 2}
})

监听器:

  watch: {// 单个监听a: function (a) {console.log(a)},b: function (b) {console.log(b)},// 多个监听'a,b': function (a, b) {console.log(a, b)}},
7.Mobx 与 Computed 结合使用

需要使用 ComponentWithStore 方法将 Component 方法进行替换

// 导入计算属性 behavior
const computedBehavior = require('miniprogram-computed').behavior
ComponentWithStore({// 注册 behaviorbehaviors: [computedBehavior],storeBindings: {store: numStore,fields: ['numA', 'numB', 'sum'],actions: ['update']}
})

ComponentWithComputed进行构建,和store建立关联、绑定,Mobx需要旧版 API

import { numStore } from '../../store/numstore'
// 需要使用导入的 ComponentWithComputed 替换 Component 方法
ComponentWithComputed({behaviors: [storeBindingsBehavior],storeBindings: {store: numStore,fields: ['numA', 'numB', 'sum'],actions: ['update']}
})
8.什么是Token

什么是 Token

Token 是服务器生成的一串字符串,用作客户端发起请求的一个身份令牌。当第一次登录成功后,服务器生成一个 Token 便将此 Token 返回给客户端,客户端在接收到 Token 以后,会使用某种方式将 Token 保存到本地。以后客户端发起请求,只需要在请求头上带上这个 Token ,服务器通过验证 Token 来确认用户的身份,而无需再次带上用户名和密码。

Token的具体流程

  1. 客户端向服务器发起登录请求,服务端验证用户名与密码
  2. 验证成功后,服务端会签发一个 Token ,并将 Token 发送到客户端
  3. 客户端收到 token 以后,将其存储起来,比如放在 localStorage 、 sessionStorage 中
  4. 客户端每次向服务器请求资源的时候需要带着服务端签发的 Token ,服务端收到请求,然后去验证客户端请求里面带着的 Token ,如果验证成功,就向客户端返回请求的数据
    在这里插入图片描述
9.用户登录-小程序登录功能
// 导入封装的模板方法
import { toast } from '../../utils/extendApi'
// 导入接口API 函数
import { reqLogin } from '../../api/user'
Page({login() {wx.login({success: async ({ code }) => {if (code) {// 获取临时登录凭证code以后,传递给开发者服务器const res = await reqLogin(code)// 登录成功后,需要将服务器响应的自定义登录存储到本地setStorage('token', data.token)} else {toast({ title: '授权失败,请重新授权' })}}})}
})import http from '../utils/http'
export const reqLogin = (code) => {return http.get('/weixin/wxLogin/{code}')
}
http://www.dtcms.com/a/279482.html

相关文章:

  • Linux连接跟踪Conntrack:原理、应用与内核实现
  • OSPF高级特性之GR
  • echarts应用到swiper 轮播图中,每次鼠标一点击图表所在slide,图表就会消失
  • LSV负载均衡
  • PostgreSQL ExecInitIndexScan 函数解析
  • k8s-高级调度(二)
  • 如何使用Cisco DevNet提供的免费ACI学习实验室(Learning Labs)?(Grok3 回答)
  • PostgreSQL 16 Administration Cookbook 读书笔记:第6章 Security
  • DLL 文件 OSError: [WinError 1401] 应用程序无法启动问题解决
  • 七、深度学习——RNN
  • HTTPS 协议原理
  • ZYNQ双核通信终极指南:FreeRTOS移植+OpenAMP双核通信+固化实战
  • 一文明白AI、AIGC、LLM、GPT、Agent、workFlow、MCP、RAG概念与关系
  • 浏览器防录屏是怎样提高视频安全性?
  • 现有医疗AI记忆、规划与工具使用的创新路径分析
  • 【Linux网络】多路转接poll、epoll
  • vue3 JavaScript 获取 el-table 单元格 赋红色外框
  • mac上用datagrip连接es
  • MFC/C++语言怎么比较CString类型最后一个字符
  • K8S的平台核心架构思想[面向抽象编程]
  • LVS(Linux Virtual Server)集群技术详解
  • linux 内核: 访问当前进程的 task_struct
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 架构搭建
  • C++-linux 6.makefile和cmake
  • 深入掌握Performance面板与LCP/FCP指标优化指南
  • 学习笔记——农作物遥感识别与大范围农作物类别制图的若干关键问题
  • 计算两个经纬度之间的距离(JavaScript 实现)
  • HashMap的长度为什么要是2的n次幂以及HashMap的继承关系(元码解析)
  • 前缀和题目:使数组互补的最少操作次数
  • 闲庭信步使用图像验证平台加速FPGA的开发:第十四课——图像二值化的FPGA实现