微信小程序 app.js 简单调用其他页面的方法

方法1

app.js里面

 globalData: {
        pageName:"",
    },

testfunction(){
      getApp().globalData.pageName.calledFunction()
}

被调用函数页面 test.js

Page({
   onLoad(options) {
        getApp().globalData.pageName = this
    },


calledFunction(){
       console.log("函数已被app.js调用")
    }
})

方法2

app.js文件

testfunction(){
      getApp().globalData.methodName()
}

otherPage.js文件

getApp().globalData.methodName = function() {
   console.log("这是被调用的方法");
}

Page({
   ........
   //被调用方法写在里面将报错
})

方法1如果pageName没有this值将会报错,人话就是被调用的页面需要被打开过执行生命周期函数--监听页面加载;

方法2无视方法1

你可能感兴趣的:(微信小程序,小程序)