此答案适用于 2.xx 版本,并从 2.15 开始编写。
听起来您刚刚开始使用 Ember。欢迎!我强烈建议您完成教程并阅读指南。它们是一种投资——事情会进展得更快,之后对你来说会更容易。我要告诉你的一切都在那里。它们不可跳过。
在您的用户路线中:
model() {
return this.store.findAll('users')
}
在您的用户控制器中:
actions: {
consoleLogger(thingToLog) {
console.log(thingToLog)
}
}
数据和动作从控制器和路由向下传递到组件。这是通过车把模板完成的。
将动作、模型和变量传递给组件的一般示例:
{{some-component
someControllerVariable=someControllerVariable
mode=model
consoleLogger=(action "consoleLogger")}}
然后在您的组件中,您可以使用如下操作:
<button {{action consoleLogger "string to log"}}>Log it</button>
或者像这样在你的组件 js 中使用它:
this.get('consoleLogger')("string to log");
// this.get retrieves the function and then we call it with an argument, kind of like how you'd do someOtherFunction() in plain js
由于您需要显示助手列表,因此您需要在路线中使用每个助手:
{{#each users as |user|}}
{{user-list-item
someControllerVariable=someControllerVariable
user=user
consoleLogger=(action "consoleLogger")}}
{{/each}}
在您的组件中使用该操作时,您可以将记录作为参数传递。他们将转到控制器操作,这就是您应该执行 POST/PATCH/DELETE 类型操作的地方,例如 save()。
祝你好运 :)