首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义刺激反射类

自定义刺激反射类
EN

Stack Overflow用户
提问于 2020-10-06 04:44:48
回答 1查看 530关注 0票数 2

我正在Rails 6中做一个实验项目,这个项目的目的是学习新的方法&使用新的(Ish)技术。

简而言之,我在玩先驱者基础设施,反射 & 组件,还有反射

据我所知,StimulusReflex & ViewComponentReflex期望反射类驻留在reflexes文件夹/命名空间中。

我想要达到的目标:

将我的ViewComponentReflex组件嵌套在Trailblazer概念文件夹(命名空间)中。

我尝试过的:

创建了以下文件夹结构:

  • concepts/post/component/counter.rb
代码语言:javascript
复制
class Component::Counter < ApplicationComponent

  def initialize
    @loading = false
  end

  def loading=(new_value)
    @loading = new_value
    refresh! '#loader'
  end

  def do_expensive_action
    prevent_refresh!

    self.loading = true
    wait 20
    self.loading = false
  end

end
  • concepts/post/component/counter/counter.html.erb
代码语言:javascript
复制
<%= component_controller do %>
  <div id="loader">
    <% if @loading %>
      <p>Loading...</p>
    <% end %>
  </div>

  <button type="button" data-reflex="click->Component::Counter#do_expensive_action" >Load Content</button>
<% end %>

预期结果:

我希望Rails允许使用名称空间等。当设置上面的程序&运行一个示例应用程序时,我会得到错误:uninitialized constant Component::CounterReflex。这个阶级的信赖是建立在一个宝石,但我不知道在哪里找到它。我尝试过在ViewComponentReflex中重写一些方法,但没有结果。

当我将Component::Counter移动到components文件夹(如这个例子中所示)时,代码可以工作。

是否有任何方法可以使用此堆栈重新定义/配置反射类的路由(模块)?

更新:

我的应用程序的GitHub存储库可以是在这里发现的

我在点击“计数器”按钮时看到的确切错误消息是:

代码语言:javascript
复制
StimulusReflex::Channel is streaming from StimulusReflex::Channel
06:48:27 log.1       | StimulusReflex::Channel#receive({"target"=>"Component::Counter#do_expensive_action", "args"=>[], "url"=>"http://krated.test/", "attrs"=>{"type"=>"button", "data-reflex"=>"click->Component::Counter#do_expensive_action", "data-key"=>"6b36d7d05b8737b0328d19bd2fff2679901b1736bb9e242b128e3b715aba6e87", "data-controller"=>"stimulus-reflex", "data-action"=>"click->stimulus-reflex#__perform", "checked"=>false, "selected"=>false, "tag_name"=>"BUTTON", "value"=>""}, "dataset"=>{"data-reflex"=>"click->Component::Counter#do_expensive_action", "data-key"=>"6b36d7d05b8737b0328d19bd2fff2679901b1736bb9e242b128e3b715aba6e87", "data-controller"=>"stimulus-reflex", "data-action"=>"click->stimulus-reflex#__perform"}, "selectors"=>[], "reflexId"=>"a091247b-d53b-4e63-ac59-78c72c4a3cb1", "permanent_attribute_name"=>"data-reflex-permanent", "params"=>{}})
06:48:27 log.1       | StimulusReflex::Channel Failed to invoke Component::Counter#do_expensive_action! http://krated.test/ uninitialized constant Component::CounterReflex /Users/hermann/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/inflector/methods.rb:284:in `const_get
EN

回答 1

Stack Overflow用户

发布于 2020-10-08 10:17:38

view_component_reflex的2.3.5版期望组件类名以Component结尾。如果他们不这么做,那就失败了。请参阅反射/工程#L18

因此,再次尝试调用组件:

代码语言:javascript
复制
Component::CounterCountComponent
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64219483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档