更新:似乎railo完全没有这个问题。
更新:我投票决定关闭这个问题,因为我觉得人们更关注这个问题的整体“有人有更好的想法拆分大型组件”部分(我不应该放入)那么使用 cfincludes 和 cfcomponent 的真正问题。
注意:这只是一个简化的例子,说明我正在尝试做的事情来让人们理解这个想法。
我遇到的问题是我想在 cfcomponent 中使用 cfinclude,这样我就可以将类似的方法分组到单独的文件中,以提高可管理性。我遇到的问题是当我尝试扩展另一个组件时,该组件也使用 cfinclude 来管理它的方法,如下所示。请注意,ComponentA 扩展了 ComponentB:
ComponentA
==========
<cfcomponent output="false" extends="componentb">
<cfinclude template="componenta/methods.cfm">
</cfcomponent>
componenta/methods.cfm
======================
<cffunction name="a"><cfreturn "componenta-a"></cffunction>
<cffunction name="b"><cfreturn "componenta-b"></cffunction>
<cffunction name="c"><cfreturn "componenta-c"></cffunction>
<cffunction name="d"><cfreturn super.a()></cffunction>
ComponentB
==========
<cfcomponent output="false">
<cfinclude template="componentb/methods.cfm">
</cfcomponent>
componentb/methods.cfm
======================
<cffunction name="a"><cfreturn "componentb-a"></cffunction>
<cffunction name="b"><cfreturn "componentb-b"></cffunction>
<cffunction name="c"><cfreturn "componentb-c"></cffunction>
问题是,当我尝试初始化 ComponentA 时出现错误:“例程不能多次声明。例程 a 已在不同模板中声明两次。”
这样做的全部原因是因为当您使用 cfinclude 时,它是在运行时而不是编译时求值的。
如果没有将方法移到组件本身并消除 cfinclude 的使用,我该如何解决这个问题,或者有人有更好的主意来拆分大型组件吗?
最佳答案
未测试,但我会尝试将每个函数的内容放入包含中,但在组件文件本身中定义函数。 `
<cfcomponent name="a">
<cffunction name="aa">
<cfinclude template="componenta/functiona.cfm" />
</cffunction>
</cfcomponent>
祝你好运。
关于oop - 如何使用 cfinclude 拆分组件并仍然使用继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3030056/