Polymer 观察者:我应该使用传递给函数的值,还是可以安全地使用这个.value?
我的应用程序中有如下代码:Polymer 观察者:我应该使用传递给函数的值,还是可以安全地使用这个.value?,polymer,web-component,observers,Polymer,Web Component,Observers,我的应用程序中有如下代码: observers: [ '_dataGenericObserver(userData.generic)', ], // If this.userData.generic.id is set, then the register tab is // NOT visible. In this case, the selected tab mustn't be "register"... _dataGenericObserver: funct
observers: [
'_dataGenericObserver(userData.generic)',
],
// If this.userData.generic.id is set, then the register tab is
// NOT visible. In this case, the selected tab mustn't be "register"...
_dataGenericObserver: function(){
if (this.userData.generic.id){
this.selected = 'login';
} else {
if (this.defaultRegister) {
this.selected = 'register';
} else {
this.selected = 'login';
}
}
},
这安全吗?或者我应该一直这样做:
observers: [
'_dataGenericObserver(userData.generic)',
],
// If this.userData.generic.id is set, then the register tab is
// NOT visible. In this case, the selected tab mustn't be "register"...
_dataGenericObserver: function(generic){
if (generic.id){
this.selected = 'login';
} else {
if (this.defaultRegister) {
this.selected = 'register';
} else {
this.selected = 'login';
}
}
},
。。。?
在某些情况下,我注意到在应用程序的其他部分,如果我在\u someFunc(value.subvalue)
上有一个观察者,然后在\u someFunc:function(subvalue)
中设置函数subvalue
,而this.value.subvalue
则不设置。然而,我无法复制它-抱歉
问题:
- 是否总是建议使用传递给函数的值,而不是
this.*
- 什么时候可能会发生
被设置为函数参数,而不是在子值
中此.value.subValue
this
,因为当您遇到this时,当观察者函数的myVar
参数为空时,可能无法设置.myVar
至少这是聚合物开发商的官方消息。(可能还有更多关于这方面的信息)
这适用于Polymer 1.x。不确定这是否与Polymer 2.x不同。AFAIK您应该始终明确声明并使用要在观察者中访问的依赖项,而不是使用
This
,因为当您遇到This.myVar
时,可能不会设置观察者的myVar
参数功能是
至少这是聚合物开发商的官方消息。(可能还有更多关于这方面的信息)
这适用于聚合物1.x,不确定这是否与聚合物2.x不同。谢谢,太好了。谢谢