Blogger Information
Blog 12
fans 1
comment 1
visits 19008
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
微信小程序util.js与app.js
黄小凡的博客
Original
3693 people have browsed it

    本文记录我在工作中遇到的微信小程序问题

    微信小程序目前有大小限制,最大不能超过2M,所以复用一些js文件是非常有必要的:

    目前我所了解的是以下两类方法:

第一:将js文件放入主目录的app.js文件下

    例如在app.js文件中放入函数:

add:function(a,b){
    return a+b;
  }

    此时在其他js文件中的函数中调用此函数,可以用如下方法:

var app = getApp()
Page({
    data: {
        motto: ''
      },
  onLoad: function () {
    that.setData({
    motto: app.add(3,5);
    })
  }
})

    此时motto的值就是8

第二:将js文件放入主目录的utils/util.js文件下

    例如在util.js文件中放入函数

function add(x,y){
  return x+y
}

    此时需要在module.exports里声明该函数(个人使用'声明'这个词语)

module.exports = {
  formatTime: formatTime,
  add:add
}

    此时在其他js文件中的函数中调用此函数,可以用如下方法:

var util = require('../../utils/util.js')
Page({
  data: {
    motto: ''
  },
  onLoad: function () {
    that.setData({
      motto: util.add(3,5)
    })
  }
})

此时motto的值也是8


暂时使用这两类方法。希望对大家有帮助!

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
PHP中文网 2017-07-28 15:28:15
文章不错
1 floor
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!