this问题
来源:5-14 Props --v2.6.11(四)

前端工程师666777888
2021-04-09
proxy()函数中,get和set为什么用this,而不是用target。感觉用target,代码会更清晰
----------------
vm.key访问时,会执行get方法,此时get里的this怎么就是vm了呢
------
如果vm.name = {
get: function (){this}
}
vm.name.get():此时里面的this是什么?
写回答
1回答
-
ustbhuangyi
2021-04-09
不能用 target,如果是子组件的话,通常都是在 Vue.extend 的过程执行 initProps
function initProps (Comp) {
const props = Comp.options.props
for (const key in props) {
proxy(Comp.prototype, `_props`, key)
}
}
可以看到,这里的 target 是 Comp.prototype,所以不能直接用 target,而必须要用 this,this 才可以访问到组件的实例 vm112021-04-10
相似问题
ts 报错 2683 this问题
回答 1
this指向
回答 1