You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
strats.data=function(parentVal: any,childVal: any,vm?: Component): ?Function{if(!vm){if(childVal&&typeofchildVal!=='function'){process.env.NODE_ENV!=='production'&&warn('The "data" option should be a function '+'that returns a per-instance value in component '+'definitions.',vm)returnparentVal}returnmergeDataOrFn(parentVal,childVal)}returnmergeDataOrFn(parentVal,childVal,vm)}
The text was updated successfully, but these errors were encountered:
一个组件的 data 选项必须是一个函数,因此每个实例可以维护一份被返回对象的独立的拷贝
这是因为,通过函数返回数据对象,保证了每个组件实例都有一个唯一的数据副本,避免了组件间数据互相影响。后面讲到 Vue 的初始化的时候大家会看到,在初始化数据状态的时候,就是通过执行 strats.data 函数来获取数据并对其进行处理的。
源码
The text was updated successfully, but these errors were encountered: