wepy 小程序开发(interceptor拦截器 && WXS)

WePY全局拦截器可对原生API的请求进行拦截。app

import wepy from 'wepy'; export default class extends wepy.app { constructor () { // this is not allowed before super()
 super(); // 拦截request请求
        this.intercept('request', { // 发出请求时的回调函数
 config (p) { // 对全部request请求中的OBJECT参数对象统一附加时间戳属性
                p.timestamp = +new Date(); console.log('config request: ', p); // 必须返回OBJECT参数对象,不然没法发送请求到服务端
                return p; }, // 请求成功后的回调函数
 success (p) { // 能够在这里对收到的响应数据对象进行加工处理
                console.log('request success: ', p); // 必须返回响应数据对象,不然后续没法对响应数据进行处理
                return p; }, //请求失败后的回调函数
 fail (p) { console.log('request fail: ', p); // 必须返回响应数据对象,不然后续没法对响应数据进行处理
                return p; }, // 请求完成时的回调函数(请求成功或失败都会被执行)
 complete (p) { console.log('request complete: ', p); } }); } }

 

WXS函数

// mywxs.wxs
 module.exports = { text: 'This is from wxs', filter: function (num) { return num.toFixed(2); } };
// index.wpy

<template>
  <text>{{m1.text}}</text>
  <text>{{m1.filter(num)}}</text>
</template>

<script> import wepy from 'wepy'; import mywxs from '../wxs/mywxs.wxs'; export default class Index extends wepy.page { data = { num: 10 }; wxs = { m1: mywxs } }; </script>
  1. wxs是基于原生的wxs去实现的,只是经过编译把如今的语法编译为原生语法。
  2. wxs必须是外链文件。而且后缀为.wxs
  3. wxs引入后只能在template中使用,不能在script中使用。
相关文章
相关标签/搜索