支持登录内session级别的缓存请求

场景描述

有一些请求,希望请求后缓存下来,下次请求直接使用缓存

实现思路

使用 Util.requestCacheWrapper,将 this.request 转成登录内session级别的缓存请求

实现步骤

在页面的 constructor 方法中,定义缓存请求,代码如下

this.sessionCacheRequest = Util.requestCacheWrapper(this.request).bind(this);  

组件按下面的代码写

this.sessionCacheRequest = Util.requestCacheWrapper(this.page.request).bind(this.page);  

在js中引用 util.js

import Util from "$UI/wxsys/lib/base/util";

使用缓存请求的案例如下

loadDynamicRoles(contextKey){
    return new Promise((resolve,reject)=>{
        this.sessionCacheRequest({
            url: "/entry/authorize/currentUserRoles?contextKey=" + contextKey
        }).then((result) =>{
            this.dynamicInfo[contextKey]["roles"] = result.data;
            this.dynamicRolesLoaded.set(true);
            resolve(result.data);
        }).catch(()=>{
            resolve([]);
        })
    });
}

results matching ""

    No results matching ""

    results matching ""

      No results matching ""