英文:
how is the rails way include all user custom helper files in view component
问题
class ApplicationViewComponent < ViewComponent::Base
include ApplicationHelper
include AllOtherUserCreatedHelperFiles
end
class FooComponent < ApplicationViewComponent
end
英文:
class ApplicationViewComponent < ViewComponent::Base
include ApplicationHelper
end
class FooComponent < ApplicationViewComponent
end
How can I include not only ApplicationHelper but also user created all helper files in view component?
答案1
得分: 3
你不包括所有的辅助功能。
使用 helpers
代理来访问你的 Rails 辅助功能。
class UserComponent < ViewComponent::Base
def profile_icon
helpers.icon :user
end
end
如果想简化调用,可以使用 delegate
:
class UserComponent < ViewComponent::Base
delegate :icon, to: :helpers
def profile_icon
icon :user
end
end
只在实际需要时包含辅助模块。
英文:
You don't include all the helpers.
Use the helpers
proxy instead to access your Rails helpers.
class UserComponent < ViewComponent::Base
def profile_icon
helpers.icon :user
end
end
And use delegate
if you want to simplify the calls:
class UserComponent < ViewComponent::Base
delegate :icon, to: :helpers
def profile_icon
icon :user
end
end
Only include the helper modules when actually needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论