How to add a global module in Mini Program?

problem description

js in Mini Program, you can write a js as a module file, and then introduce it in the corresponding js file with require, but there are some modules that I need to use in a lot of js files, in that case, it is particularly troublesome to introduce into each js file, is there any way to introduce it in app.js and then use it directly in other js files.

the environmental background of the problems and what methods you have tried

I have tried to introduce it into the app.js file ( const requestTool = require (". / config.js") ). After introducing the module, I wrote down the requestTool, in the App of app.js and can be used in other js files through app.requestTool (app from getApp ()). I want to know if there is a better way to achieve this function.

related codes

app.js

const requestTool = require("./config.js");
// import requestTool from "./config.js"
//app.js
App({
  requestTool,

other js files
const app = getApp ();
app.requestTool

Oct.22,2021

should be gone, and the official globalData is a similar example.
this has the advantage of loading on demand every time you write, and require, it when you need it, rather than loading it all when you don't need it.
if it is introduced once at the entrance and there is no need to write it later, it must be a global introduction, regardless of whether it is used or not.


Global module can be written directly in app.js and referenced when other pages are to be referenced

var app = getApp(); //
app.

can be called, and global variables can be defined directly in the globalData of app.js

Menu