微信小程序wxs的使用(当页面数据渲染前添加js *** 作)

微信小程序wxs的使用(当页面数据渲染前添加js *** 作),第1张

程序的wxs功能可以让wsmxl可以调用和编写js,基本上wxs和JS无关系,只是语法形式很相似。

如下写了两个关于时间的函数,并将它们导出,

<wxs module="m1">

var getMax = function(flightDate) {

    var now = getDate()getDate();

    var flDate = getDate(flightDate)getDate();

    if( now < flDate ){

      return '+1';

    }else{

      return '';

    }

}

var formartTime = function(flightDate,format){

  if(flightDate){

    var realDate = getDate(flightDate);

    function timeFormat(num) {

      return num < 10 '0' + num : num;

    }

    var date = {

      "Y": timeFormat(realDategetFullYear()),

      "M": timeFormat(realDategetMonth() + 1),

      "d": timeFormat(realDategetDate()),

      "h": timeFormat(realDategetHours()),

      "m": timeFormat(realDategetMinutes()),

      "s": timeFormat(realDategetSeconds()),

      "q": Mathfloor((realDategetMonth() + 3) / 3),

      "S": realDategetMilliseconds(),

    };

    if (!format) {

      format = "yyyy-MM-dd hh:mm:ss";

    }

    if( format == 'hh:mm' ){

        return dateh+':'+datem;

    }else{

        return dateh+':'+datem;

    }

  }else{

    return false;

  }

}

moduleexportsgetMax = getMax;

moduleexportsformartTime = formartTime;

</wxs>

可在页面添加如下使用:

m1formartTime();  m1getMax();

1、首先预备好外部想要引入的外部文件,命名为utiljs,并且填充固定的文件内容(普通是固定的库)。

2、其次打开utiljs ,持续填写重要内容将要利用的方法用moduleexports给暴显露来。

3、然后将外部js放在指定的文件夹utils里(utils 规定寄存js库和数字格式化文件)。

4、最后在想要用到这个方法的js里面 require这个js,然后调用即可。

wxml部分

<van-uploader file-list="{{ picture_list }}" preview-full-image="{{false}}" max-size="2048000" max-count="9" multiple="{{true}}" bind:after-read="afterRead" bind:delete="deleteImg" />

js部分

afterRead:function(event) {

const { file } = eventdetail;

    // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式

    var obj={}

objpath=file;

    thisuploadimg(obj);

},

uploadimg:function(data) {

wxshowLoading({

title:'上传中',

        mask:true,

    })

var that =this,

        i = datai datai :0,

        success = datasuccess datasuccess :0,

        fail = datafail datafail :0;

    let userInfo=wxgetStorageSync('userInfo');

    let peopleId=userInfopeopleId;

    var picture_list = thatdatapicture_list;

    wxuploadFile({

url: baseUrl +'impl/uploadImg',

        filePath: datapath[i]path,

        name:'file',

        formData: {

'type':1,

            'peopleId':peopleId,

        },

        success: (res) => {

wxhideLoading();

            success++;

            let obj =  JSONparse(resdata)data;

            if(objhasOwnProperty('url')){

picture_listpush({

url:objurl,

                    id:objid

                });

                thatsetData({

picture_list

})

}

},

        fail: (err) => {

wxhideLoading();

            wxshowToast({

title:'上传失败!',

                icon:'none'

            })

fail++;

        },

        complete: () => {

i++;

            if (i == datapathlength) {//当传完时,停止调用

            }else {//若还没有传完,则继续调用函数

                datai = i;

                datasuccess = success;

                datafail = fail;

                thatuploadimg(data);//递归,回调自己

            }

}

});

},

deleteImg(e) {

let index=edetailindex;

    consolelog(index);

    let {picture_list}=thisdata;

    let that=this;

    wxshowModal({

content:'确定删除?',

        success: (res) => {

if (resconfirm) {

picture_listsplice(index,1);

                thatsetData({

picture_list

                })

consolelog(thatdatapicture_list);

            }else if (rescancel) {

consolelog('用户点击取消')

}

},

    })

},

微信小程序飞机大战外部引用方法:

1、直接在标签里加上style样式,加上背景图:。

2、使用外部引入的话,直接使用backqround会报渲染层错误,可以将上传到服务器,然后在使用background。

3、直接使用image标签引用。

以上就是关于微信小程序wxs的使用(当页面数据渲染前添加js *** 作)全部的内容,包括:微信小程序wxs的使用(当页面数据渲染前添加js *** 作)、微信小程序开发者工具如何在js中导入图片、微信小程序,引用vant-weapp库,上传组件uploader,支持多选等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10212192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存