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

globalData 在别的文件里怎么用? #31

Closed
xueenda opened this issue Jan 22, 2017 · 8 comments
Closed

globalData 在别的文件里怎么用? #31

xueenda opened this issue Jan 22, 2017 · 8 comments

Comments

@xueenda
Copy link

xueenda commented Jan 22, 2017

如题
给个例子 谢谢

@Gcaufy
Copy link
Collaborator

Gcaufy commented Jan 23, 2017

this.$wxapp 可以取到app
所以this.$wxapp.globalData 就是你定义在 app里面的globalData

@Gcaufy Gcaufy closed this as completed Jan 23, 2017
@jiayx
Copy link

jiayx commented Mar 31, 2017

我在测试的时候发现其他页面里 this.$wxapp.globalData 取不到,要这样才行 this.$wxapp.$app.globalData
是新版有改动吗?this.$wxapp 下只有方法,没有属性。

@jawalkwan
Copy link

@jiayx 少年,你试试this.$parent.globalData

@jiayx
Copy link

jiayx commented Mar 31, 2017

这个可以。。回复好快,多谢了!

@jawalkwan
Copy link

@jiayx 怪github,给我推邮件……我也不想的(摊手)

@Gcaufy
Copy link
Collaborator

Gcaufy commented Mar 31, 2017

@jiayx
如果当前组件是page,那么this.$parent是$app
如果当前组件是component, 那么this.$parent是父层component,this.$root是顶层的page,所以你可以这么写this.$root.$parent

另外还有一种方法:wepy.$instance 也可以

@jiayx
Copy link

jiayx commented Mar 31, 2017

@Gcaufy OK,了解了,回答的很详细,非常感谢!

@silent-tan
Copy link

$instance 的api我在官网文档找不到

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

No branches or pull requests

5 participants