Ember:this.modelFor不是一个函数(在组件中)

时间:2016-03-29 16:45:32

标签: ember.js

关于Ember方法的问题modelFor

我在组件中使用它的尝试失败了:

//info component

model(params){
return this.store.createRecord('info'); 
},

user: this.modelFor('user'),

此代码会破坏整个应用程序,并显示以下错误:

ember.debug.js:4875 Uncaught TypeError: this.modelFor is not a function

我发现自己经常想要在组件中使用方法,或者想要在我的组件逻辑中访问多个模型 - 有没有人知道一个很好的资源来教育自己这些最佳实践?

谢谢!

1 个答案:

答案 0 :(得分:2)

modelFor 不是组件中的函数。您必须通过正在实现组件的模板中的参数将模型传递到组件中。

{{my-component model=model}}

Documentation for Component

modelFor 是路线中的一项功能。

Documentation for Route

返回多个模型

路线

model: function() {
  let model1 = this.modelFor('x'),
      model2 = this.modelFor('y');
  return {
    model1: model1,
    model2: model2
  };
}