比方說,我在一線的簡單組件與項目的列表如何從閃爍的組件中獲取父上下文?
<todo-list @items={{ items }}></todo-list>
template.hbs
<ul>
{{#each @items key="@index" as |item|}}
<li onclick={{ action clickme }}>{{ item }}</li>
{{/each}}
</ul>
component.ts
import Component, { tracked } from '@glimmer/component';
export default class TodoList extends Component {
constructor(options) {
super(options);
}
clickme() {
// how do I access the parent context from here?
}
}
即使我通過在行動來自父母
<todo-list @items={{ items }} @rootclickme={{ rootclickme }}></todo-list>
更新,template.hbs
<ul>
{{#each @items key="@index" as |item|}}
<li onclick={{ action @rootclickme }}>{{ item }}</li>
{{/each}}
</ul>
在我的外component.ts
rootclickme() {
// I don't have access to parent variables here either?
// only what's within the scope of the function itself?
}
我試圖做的是有一個表中的組件。當單擊一個列表項時,我希望它將一個單擊事件放到頂部,以便父組件可以決定隱藏列表並顯示該選定項目的更詳細視圖。
我該如何着手做這件事?在反應我路過
注:我沒有使用全部ember.js,只是glimmer.js獨立
當你在綁定動作時,它應該是'@rootclickme = {{action rootclickme}}'。使用'action'助手可以確保回調函數'this'綁定到正確的組件。 – locks
你似乎也削減了你的帖子? 「在反應中,我會通過」。 – locks