search
HomeWeChat AppletMini Program DevelopmentWeChat applet refers to methods in other js

In the WeChat mini program, we can know the directory structure of the mini program in the WeChat official development documentation.

A mini program page consists of four files. The four files of a mini program page have the same path and file name. From this we can know that a mini program page corresponds to a js file with the same name as the page. But when there are some public methods, we want to extract them into an independent public js file. How can we achieve this?

微信小程序   引用其他js里的方法

There is an app.js file in the root directory. We can easily call the js file in this root directory through getApp().

//app.js
App({
   globaData:'huangenai'
 })

//test.jsvar app = getApp();
Page({
 onLoad: function () {
 console.log(app.globaData);
 } 
})

You can see it in the Console of the developer tools

微信小程序   引用其他js里的方法

Then when we extract some common public methods, create a new utils folder in the root directory, and create a new util.js where we can write the common methods here.

util.js

//正则判断function Regular(str, reg) {  if (reg.test(str))    return true;  return false;
}//是否为中文function IsChinese(str) {  var reg = /^[\u0391-\uFFE5]+$/;  return Regular(str, reg);
}

module.exports = {
  getRequestUrl: "http://localhost:59637",//获得接口地址  IsChinese: IsChinese,
}

//test.jsvar util = require('../../utils/util.js');
Page({
    onLoad: function () {
    console.log("判断是否为中文:"+util.IsChinese('测试'));
    console.log("输出接口url:"+util.getRequestUrl);
    }
})

You can see it in the Console of the developer tools

微信小程序   引用其他js里的方法

Note that in the Regular() method in util.js, we cannot call util.Regular() like this because we do not use module.exports to expose the module interface

If you directly When calling, an error like this will occur

thirdScriptError
util.Regular is not a function;at "pages/test/test" page lifeCycleMethod onLoad function
TypeError: util.Regular is not a function

For more WeChat mini programs, please pay attention to the PHP Chinese website for related articles citing methods in other js!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor