如果在树枝内部我有一个对象,我想测试该对象是否可以在该对象上调用名为 getName 的方法,是否存在这种情况?
我尝试执行以下操作但没有成功:
{% if lastCategory.method(getName) is defined %}
Run Code Online (Sandbox Code Playgroud)
是的,你可以这样做:
{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}
Run Code Online (Sandbox Code Playgroud)
在你的情况下,它会是这样的
{{ attribute(lastCategory, 'getName') is defined ? 'Method exists' : 'Method does not exist' }}
Run Code Online (Sandbox Code Playgroud)
查看文档