interview-answe icon indicating copy to clipboard operation
interview-answe copied to clipboard

198.[vue]组件的 data 为什么必须是一个函数?

Open webVueBlog opened this issue 4 years ago • 1 comments

[vue]

webVueBlog avatar Apr 21 '20 01:04 webVueBlog

理解:

保证组件的相对独立 若是 data 是对象,则所有组件共享同一个对象,数据会相互感染 data 用函数返回一个对象,复用组件会创建多个实例 根组件的 data 可以是一个对象,因为它不会被复用 原理

Vue.extend 中会执行 mergeOptions 方法来 合并 Vue 构造函数和子组件的 options到 vm.$options组件

webVueBlog avatar Apr 21 '20 01:04 webVueBlog