简体   繁体   English

使用 Handlebars 在 Ember Octane 中切换类的正确方法?

[英]Proper way to toggle classes in Ember Octane with Handlebars?

I want to toggle between class display on and display-off using Ember.js Octane.我想使用 Ember.js Octane 在 class 显示打开和显示关闭之间切换。 How do I do this?我该怎么做呢? Do I need an @action or @tracked?我需要@action 还是@tracked?

<img src="flower.jpg" alt="flower" class="display-on">  
or
<img src="flower.jpg" alt="flower" class="display-off"> 

This should be straight-forward in Ember (especially with Octane features).这在 Ember 中应该是直截了当的(尤其是 Octane 功能)。

As mentioned by @jrjohnson in the comment, the markup depends on how you manage the way you track the state, by which the classes are toggled.正如@jrjohnson 在评论中提到的,标记取决于您如何管理跟踪 state 的方式,通过该方式切换类。 In an ideal case, since you need to toggle the class alone, you can render the class using the conditionals in the template with the help of a JS Class property, canShow :在理想情况下,由于您需要单独切换class ,您可以借助 JS Class 属性使用模板中的条件来渲染 class, canShow

<img src="flower.jpg" alt="flower" class={{if this.canShow "display-on" "display-off"}}> 

here, canShow has to be @tracked (as you mentioned) if you need to re-render the view(template) when it's value changes.在这里,如果您需要在值更改时重新渲染视图(模板),则canShow必须是@tracked (如您所述)。

Hope you having fun learning Ember;)希望你在学习 Ember 时玩得开心;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM