Applet introduction of external modules

Introducing external module (Method)

Export

<! - Create a new file help.js utils folder -> 
<-! Help.js Export ->

<! - a, ->
module.exports.userName = "Li Yunlong"

<! - II ->
module.exports.userName = "Li Yunlong";
module.exports.run = function(){
    console.log(this.userName)
}

<-! Three, ->
module.exports.run = function(){
    console.log(this.data.userName)
    This is the data within their own js current in
}

<! - IV ->
module.exports.userName = "Li Yunlong";
module.exports.age = 19;
module.exports.run = function(){
  console.log(this.userName,this.age)
}

Importing

<-! JS folder Import ->
const help = require("../../utils/help.js")

<! - a, ->
onLoad: function (options) {
    console.log(help.userName)  
},

<! - two, -> 
< Button bindtap = "RUN" > Point I die </ Button >
run(){
    help.run()
},

<! - three, -> 
< Button bindtap = "RUN" > Point I die </ Button >
run:help.run,

<!-- 四、 -->
run:help.run.bind(help),

Note:

<! - get application instance ->
const app = getApp()
<! - after obtaining js application example you can use global variables (the definition requires app.js <Age> </ Age>) ->
onLoad: function (options) {
    console.log(app.globalData.age)
},

merge:

page({
        ...config,
        ...help
    })
    <! - All methods can be written out in this way merge, generally do not use ye ->

 

Guess you like

Origin www.cnblogs.com/xiaoyaolang/p/11961387.html