Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在onLaunch时如何获取到微信小程序实例(getApp())? #753

Closed
etflly opened this issue Jan 11, 2018 · 10 comments
Closed

在onLaunch时如何获取到微信小程序实例(getApp())? #753

etflly opened this issue Jan 11, 2018 · 10 comments
Assignees

Comments

@etflly
Copy link

etflly commented Jan 11, 2018

在原生的写法中,onLaunch中可以通过this指针获取到小程序实例。但是wepy里直接getApp()或者wepy.$instance.$wxapp获得的是undefined

这种情形下有没有方法可以直接获得小程序实例?

@Gcaufy
Copy link
Collaborator

Gcaufy commented Jan 12, 2018

  1. onLaunch 的阶段是拿不到小程序实例的
  2. this 只 wepy.app 的实例而不是小程序实例

@Gcaufy Gcaufy closed this as completed Jan 12, 2018
@etflly
Copy link
Author

etflly commented Jan 12, 2018

@Gcaufy
原生的小程序是可以再onLaunch阶段拿到小程序实例的。如下图:
1
运行得到:
2

@JiajunW
Copy link

JiajunW commented Feb 7, 2018

问题还没解决。使用 wepy 定义 app 时 this.$wxapp 是 undefined

原生小程序在定义 App 的时候并不能通过 getApp() 来拿到实例,而是需要直接用 this 来访问到。参考:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/app.html

@wuzhuzhu
Copy link

这个问题同样困扰了我 登陆信息放在哪里很头疼

@jayconscious
Copy link

希望可以尽快解决哈,不过现在我是放在storage中的@wuzhuzhu

@frontbear
Copy link

frontbear commented Mar 6, 2018

@Gcaufy 我也遇到了同样的问题,麻烦下个版本解决下,谢谢

@ZhuoYitao
Copy link

@wuzhuzhu 自己定义个全局对象,放里面就好了

@asins
Copy link

asins commented Jul 4, 2018

@ZhuoYitao 你说的定义一个全局变量如何理解?

比如我的需求是在app.wpy中实现一个功能组件 想在onLaunch中定义

onLaunch(){
    this.loginSdk = new LoginSdk();
    const app = getApp(); // 这也是失败的app为undefined
}

page/demo.wpy页面中想通过getApp().loginSdk使用这个实例是失败的,在onLaunch中this为app.js的实例,getApp()结果为空,这很尴尬。

@iiewad
Copy link

iiewad commented Aug 25, 2018

this.$parent.globalData.userInfo

@fplun
Copy link

fplun commented Nov 19, 2019

@Gcaufy
原生的小程序是可以再onLaunch阶段拿到小程序实例的。如下图:
1
运行得到:
2

这是什么神仙操作

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants