Tencent / omi

Web Components Framework - Web组件框架

Home Page:http://omijs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

charliex2 opened this issue · comments

理论上 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

遇到同样的问题了

遇到同样的问题了

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