父
check() {
if(typeof this.$.child.function() == 'function') {
// do something
}
}
如何检查子功能是否存在?
抛出
Uncaught TypeError:this。$。child.function不是函数
答案 0 :(得分:1)
Probably the main problem with your code is that you are actually calling the function with the parenthesis ()
and then comparing the result with the string 'function'
.
To check the existence remove the parenthesis:
if (this.$.child.function == 'function') { /* ... */ };
However the error could also be caused by this.$.child
being undefined when called. This can happen if you're trying to access your component's static node map (this.$
) when Polymer hasn't initialised it yet.
The map is accessible in the ready
lifecycle callback: see here.
答案 1 :(得分:0)
Assuming you are not using the reserved keyword 'function' as a method name the following should work perfectly fine:
check() {
if(typeof this.$.child.someFunDefinedInChild == 'function') {
//do work
}
}