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

Omix 在create.component 创建组件页面的时候,数据被初始化了 #524

Open
fanlei1988 opened this issue Jan 6, 2020 · 5 comments

Comments

@fanlei1988
Copy link

在使用 create.component 创建组件页面的时候,使用onLoad接收参数并赋值给data,在ready或者异步回调的时候data初始化了

实例代码:

create.Component(store, {
     data: {
         id: '',
         name:''
     },
     lifetimes: {
		ready: function() {
                        //this.data 已经还原了
			console.log(this.data)
		},
	},
      methods: {
		onLoad: function(options) {
			//扫码进入
			if (options.scene) {
                                //假写
				const xxx=options.scene
			}
                     this.setData({
                           id:111,
                           name:2222
                      })
                     //走了个异步请求
                     User.get(xx).then((res)=>{
                           console.log(this.data)  //这里也被初始化了
                     })
                }
})

请问这种情况怎么处理比较好,因为我需要异步请求之后拿到数据,在处理其他逻辑,同时又需要接收的参数

@fanlei1988
Copy link
Author

@dntzhang 麻烦了

@dntzhang
Copy link
Collaborator

dntzhang commented Jan 6, 2020

不懂你的目的

@fanlei1988
Copy link
Author

我的目的很简单,就是在走完异步的时候,我可以拿到之前付给私有变量的值。

@luyi18
Copy link

luyi18 commented Mar 2, 2020

@fanlei1988 看源码发现,如果用component的方式创建页面的话,框架会在component的ready钩子时,把初始的data重新setData。因为页面的onLoad钩子是在ready钩子调用之前调用的,所以如果在onLoad中有setData的操作,可能就会被之后框架调用的ready给初始化

@luyi18
Copy link

luyi18 commented Mar 2, 2020

@dntzhang 不知道这里create.component跟create.page在创建页面的时候,为什么不一样?

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

3 participants