微信小程序中页面引入js文件

一、引入util中的util.js文件:

utils.js可以存储全局的方法(function)、变量(const url)等,类似于可以实现公共存储。

首先,来看utils.js中:

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/*
*配置全局的url
*/
const apiUrl = "http://192.168.0.109:8081/form";

//导出方法或者常量
module.exports = {
  formatTime: formatTime,
  apiUrl: apiUrl
}

在要使用的页面引入:

//引入utils.js
// var util = require('../../utils/util.js');
Page({

})

二、引入第三方的js插件(例如wxCharts .js):

引入:

// pages/stock/stock.js
//加载插件
var wxCharts = require('wxcharts.js');

Page({
  data: {},
  onLoad: function (options) {},
})
发布了128 篇原创文章 · 获赞 250 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/XU441520/article/details/103380213