[英]Open one component instance at a time in Ember Octane
Let's say we have component answer
that has isOpen
property.假设我们有具有isOpen
属性的组件answer
。 By default isOpen
is false
and when the user is clicking on the answer
we are changing the isOpen
to true
.默认情况下isOpen
为false
,当用户单击answer
时,我们将isOpen
更改为true
。
We are showing the list of answers.我们正在显示答案列表。 How can we close the first answer
when the user clicks on second answer
?当用户点击第二个answer
时,我们如何关闭第一个answer
?
I was thinking about creating a service answersSync
with globalIsOpen = false
.我正在考虑使用globalIsOpen = false
创建服务answersSync
。
I would suggest keeping track of which answer is open in your parent component, the one that lists the answers.我建议跟踪在您的父组件中打开哪个答案,即列出答案的那个。 Then your parent template would look something like:然后你的父模板看起来像:
{{#each this.answers as |answer|}}
<Answer
@answer={{answer}}
isOpen={{eq this.currentlyOpenAnswer answer}}
{{on "click" (fn this.openAnswer answer)}}
/>
{{/each}}
(then have code in the parent component's js that keeps track of currentlyOpenAnswer
, responding to the openAnswer
action) (然后在父组件的 js 中有跟踪openAnswer
currentlyOpenAnswer
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.