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
encountermm, lucas-yg, Dyrixu, runkid1, quanhuan2011 and 59 moresundeheng and simple5960wangjinquan5 and sundehengsundehengwangjinquan5, XCong, sundeheng, zby-66, shenbeixinqu and 1 moresundeheng
Activity
zyronon commentedon Jul 2, 2019
不可以,同名会报错:The computed property "xxxx" is already defined in data
llccing commentedon Jul 4, 2019
https://github.com/vuejs/vue/blob/dev/src/core/instance/state.js#L202 此处校验不会通过
Myh-cs commentedon Jul 9, 2019
不能同名 因为不管是计算属性还是data还是props 都会被挂载在vm实例上,因此 这三个都不能同名
remip518 commentedon Aug 21, 2019
不可以,写在计算属性中的数据名称,不能在data中定义
wush12 commentedon Nov 5, 2019
不可以,因为初始化vm的过程,会先把data绑定到vm,再把computed的值绑定到vm,会把data覆盖了
ajh99990 commentedon Dec 4, 2019
莫名其妙的问题。可以同名,但data会覆盖methods。并且本就不该同名,同名说明你命名不规范。
然后解释为什么会覆盖,因为Props、methods、data、computed、watch都是在initState函数中被初始化的。初始化顺序就是我上面给出的顺序,本质上这些都是要挂载到this上面的,你如果重名的话,后面出现的属性自然而然会覆盖之前挂载的属性了。如果你的eslint配置比较严格的话,同名是编译不通过的。
songkangle0826 commentedon Jun 1, 2020
其实是可以同名的, 不是先把data绑定到vm,初始化顺序是这样的,先computed,methods,data,props
Good-XiaAo commentedon Sep 6, 2020
为什么会有这样的问题?有点脑残了吧
TieHanHanYa commentedon Oct 25, 2020
正确顺序:props、methods、data、computed、watch、
laozhan0000 commentedon Nov 11, 2020
不可以,会报错,
[Vue warn]: The computed property "a" is already defined in data.
crush2020 commentedon Jan 28, 2021
不能同名,不管是方法,计算属性还是data最后都会挂载到vue实例上去,如果同名了就会把其中一条覆盖掉,那你写的计算属性,方法,在data中定义的数据,就没有什么意义了
hyj443 commentedon Oct 27, 2021
在initComputed执行时,会先看看当前vm实例的原型链上有没有这个key,用的in操作符判断
如果没有,那就创建computed
如果有,是不会创建的,并会看看它在哪里被你定义过,然后依次看看是不是在data props methods里被定义了,告诉你别和他们重名了。
sc950828 commentedon Sep 24, 2022
https://juejin.cn/post/7139788012925222919
wenjiechen0913 commentedon Apr 27, 2023
wenjiechen0913 commentedon May 6, 2023
人家说的初始化顺序没错,调用顺序就是这样的
wenjiechen0913 commentedon May 6, 2023
可以同名的吧,只是会告警而已,又不会报错。