How can wepy get the app instance in a js file?

question 1:
the wepy document says that in the Page page instance, the App instance can be accessed through this.$parent. What should I do if I want to access an app instance in a js?

wepy.login({
  success: function({ code }) { 
    ajax("user/login", {
      code
    }).then(res => {
      if (res) {
        wx.setStorageSync("user_id", res.user_id)
        wx.setStorageSync("auth_token", res.auth_token)
      }
      // res.store_id  res.store_nameapp.globalData
      getDefaultStore().then(res => {
        fn()
      })
    })
  }
})

question 2:
wepy document says:
/ / how to use WePY, you need to enable Promise support. Refer to the development specification section
wepy.request ("xxxx"). Then ((d) = > console.log (d));
1). How to enable Promise support?
2. Can all native Wechat api be called in the way of wepy.apiName. For example, wx.login can use wepy.login, wx.setNavigationBarTitle can use wepy.setNavigationBarTitle

Mar.28,2021

the owner of the building can be obtained through wepy.$instance.globalData , and the personal test can be


.

question 1:

  • if you are writing a modular js file, you can pass in this.$parent to access the app instance when you load the module

question 2:


question 1:

home.wpy
import login from '../utils/login'
onLoad(opts) {
  login.call(this, this.getHomeData)
}
------------------------------------------------
login.js
const login = function(fn) {
  console.log(this)  // undefined
  console.log('start login')
  wepy.checkSession({
    success: function(res) {
      console.log('login success')
      if (!wx.getStorageSync('auth_token')) {
        login$1.call(this, fn)
      } else {
        if (fn && typeof fn === 'function') {
          fn()
        }
      }
    },
    fail: function(res) {
      console.log('login fail')
      login$1.call(this, fn)
    }
  })
}
export default login
Menu