search
HomeWeChat AppletMini Program DevelopmentExample of external reference of js file in WeChat applet

Example of external reference of js file in WeChat applet

Mar 15, 2018 pm 05:00 PM
javascriptExampleApplets


  • # As a common project development habit, I want to extract highly reusable code, and just pass parameters and call wherever needed. I am new to WeChat. In the mini program circle, after all, it will be restricted by new specifications. Here we will sort out the reference and use of simple external public js files.

Usage

  • Take the page jump function in a personal development project as an example to simplify the code. The reference steps are as follows:

①. Processing of external js files

  • For the referenced external public js files, you can define the required functions by yourself. The key point is that below You need to use module.exports to expose the method so that it can be used by other js files

  • exports: Through this attribute, the private variables and functions of this module can be shared externally

  • Recommended reference article: WeChat Mini Program (Modular)
    For the official explanation, please refer to: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxs/01wxs-module.html

Example of external reference of js file in WeChat applet

②. require to reference the js file

  • In the current js file, use the require() method to correctly pass in the path file

var common_js = require('../../utils/common.js')

Personal testing found that for references to external files, please try to use relative paths
Moreover, the calling position of the require() method is not restricted. My personal habit is to start quoting it at the top of the file.

  • In js Business logic processing location, calling external methods (pay attention to whether relevant parameters need to be passed)

Example of external reference of js file in WeChat applet

③. Front end wxmlReference code

  • Here is a simple tag usage on the front end. By clicking on the <span></span> tag below, you can execute the js in bindFun()Method

  <span bindtap="bindFun" data-url="favor/index">
    <image src="{{imgServer}}/dbfl.jpg"></image>我的  </span>

Related recommendations:

Recommended 7 articles about external references

The above is the detailed content of Example of external reference of js file in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),