Home  >  Article  >  WeChat Applet  >  Example of external reference of js file in WeChat applet

Example of external reference of js file in WeChat applet

小云云
小云云Original
2018-03-15 17:00:587528browse


  • # 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