微信小程序官方文档上使用require来引入模块
引入模块。返回模块通过 module.exports 或 exports 暴露的接口
引用一下官方例子
// common.js
function sayHello(name) {
console.log(`Hello ${name} !`)
}
function sayGoodbye(name) {
console.log(`Goodbye ${name} !`)
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye
var common = require('common.js')
Page({
helloMINA: function() {
common.sayHello('MINA')
},
goodbyeMINA: function() {
common.sayGoodbye('MINA')
}
})
使用es6的export与import返回和引入模块
globalConfig.js
const config = {
//线上版本号
version: "1.0.1"
}
export { config }
index.js
import {
config
} from '../../globalConfig.js'
export除了上面的一种写法还有
- export写法一
export const config = {
//线上版本号
version: "1.0.1"
}
export let func = function(){
console.log("func")
}
- export写法二
const config = {
//线上版本号
version: "1.0.1"
}
let func = function () {
console.log("func")
}
export {
config,
func
}
对应的import
import {
config,
func
} from "../../globalConfig.js"
- 给export或者import的数据起别名
const config = {
//线上版本号
version: "1.0.1"
}
let func = function(){
console.log("func")
}
export { config,func as func1}
import {
config as config1,
func1
} from "../../globalConfig.js"