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

create.Component data 初始化与 attached() 执行的顺序错误 #585

Open
charliex2 opened this issue Jan 5, 2021 · 2 comments
Open

Comments

@charliex2
Copy link

charliex2 commented Jan 5, 2021

理论上 Component 中应该先初始化 data 中的值,然后在触发 attached() 函数。

在组件完全初始化完毕、进入页面节点树后, attached 生命周期被触发。此时, this.data 已被初始化为组件的当前值。这个生命周期很有用,绝大多数初始化工作可以在这个时机进行。

但是使用了 create.Component 后似乎是先执行 attached(),然后初始化 data.

下面我贴一下我的 Demo

// index/component/index.js
import create from '../../omix/create'
import store from '../../store.js'

create.Component(store,{
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    msg: "hello",
  },
  lifetimes: {
    attached() {
      this.setData({
        msg: 'world'
      })
      console.log("attached:", this.data.msg)
    },
    ready() {
      console.log("ready:", this.data.msg)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

  },
  observers: {
    msg(val){
      console.log("msg observers: ", val)
    }
  }
})

输出:

msg observers:  world
index.js:24 attached: world
msg observers:  hello
index.js:27 ready: hello

完整的小程序代码片段:

https://developers.weixin.qq.com/s/Qlghs8mt7FnU

如果直接使用 Component() 函数构造组件则输出如下所示,符合预期。

msg observers:  world
index.js:24 attached: world
index.js:27 ready: world
@734208213
Copy link

遇到同样的问题了

@charliex2
Copy link
Author

遇到同样的问题了

我们自己魔改了一下可以用了
我有点诧异..... 官方出的竟然会搞错的生命周期

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