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

传参并返回上一页时数据没变化 #24

Closed
xrjxs opened this issue Dec 15, 2018 · 3 comments
Closed

传参并返回上一页时数据没变化 #24

xrjxs opened this issue Dec 15, 2018 · 3 comments

Comments

@xrjxs
Copy link

xrjxs commented Dec 15, 2018

上一页定义的数据:

data: {
    test: null
}

当前页设置上一页数据:

      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
          test: 'some test',
      })

上一页如果在wxml里有用到 {{ test }} ,数据确实发生了变化 ,但是当返回上一页时,console.log(this.data.test) 结果却是默认值 null 没有变化

用原来的 Page({}) 打印data数据是有变化的,用了 Page.define({}) 数据就为null了

@imyelo
Copy link
Member

imyelo commented Dec 16, 2018

通过 Tina.Page.define({}) 构造的实例可以理解为是 Page({}) 实例的一层代理。

而小程序全局方法 getCurrentPages() 返回的是原生 Page({}) 构造的实例列表 —— 如果在这些实例上直接执行 setData() 则绕过了 Tina.Page 的代理,虽然最终页面上的数据依旧会发生变化,但新的数据并不会同步到 Tina 的代理实例上。

在不得不使用 getCurrentPages() 的情况下,可以通过挂载在原生实例上的 __tina_instance__ 访问 Tina 的代理实例,比如上面的例子:

  let pages = getCurrentPages()
  let prevPage = pages[pages.length - 2]
-  prevPage.setData({
+  prevPage.__tina_instance__.setData({
    test: 'some test',
  })

@imyelo imyelo closed this as completed Dec 16, 2018
@imyelo imyelo reopened this Dec 16, 2018
@imyelo
Copy link
Member

imyelo commented Dec 16, 2018

通常如果需要跨页面修改 / 访问数据,推荐引入全局状态管理器(如 Redux),而避免使用 getCurrentPages()

@imyelo
Copy link
Member

imyelo commented Dec 16, 2018

v1.5.0 released.

现在你也可以通过 import { getCurrentPages } from '@tinajs/tina 代替全局的 getCurrentPages API,访问由 Tina Page 构造的当前页面栈。

相关文档:

@imyelo imyelo closed this as completed Dec 16, 2018
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

2 participants