如果尚未设置状态变量,如何仅在应用商店中设置状态变量



我正在使用Hyperstack存储,在我的组件before_mount中,我想做:

before_mount do
  BridgeStore.show_card_sample ||= true
end

在商店里:

class BridgeStore < HyperStore
  class << self
    state_accessor :show_card_sample
  end
end

但是,每次呈现此类型的组件时都会触发条件赋值||=

我知道我可以通过在商店state_accessor :is_set中设置一个状态变量来解决这个问题,并且只有在尚未设置的情况下才设置其他变量,但我想知道是否有更好的方法解决这个问题?

您应该将初始化的逻辑移动到您的商店中。 请记住,在 Ruby 中,可以在定义类时初始化类实例变量:

class BridgeStore < HyperStore
  @show_card_sample = true
  class << self
    state_accessor :show_card_sample
  end
end

最新更新