1

我正在使用 Ember 2.3。

就像标题说的那样,我试图将一个计算属性从我的控制器传递给我的组件。我不知道我做错了什么,但它对我不起作用。这就是我正在做的事情:

在我的控制器中:

import Ember from 'ember';

export default Ember.Controller.extend({
  someProperty: 'someValue',

  myComputedProperty: Ember.computed('someProperty', function(){
    return {property: this.get('someProperty')};
  })

});

我的组件:

export default Ember.Component.extend({
   myProperty: {}
});

在我的模板中:

{{my-component myProperty=myComputedProperty}}

我看到的是我myProperty的 inmy-component总是设置为{}. 它永远不会得到我在模板中提交的值。注意我还尝试在我的控制器上将属性定义为字符串文字并将其提交,但它也无法识别。另外值得注意的是,我最初确实尝试在我的路由中定义计算属性,但我发现我可以访问它的唯一方法是它是在模型钩子本身中定义的,例如:

model(params) {
   return {
   myComputedProperty: Ember.computed()...........
  };
}

但这对我不起作用,因为我需要来自控制器的值,而这些值在调用模型钩子时不可用。

我不知道我在这里做错了什么。我敢肯定这很简单,但我的想法已经不多了。谁能告诉我我在这里做错了什么?以任何方式将计算属性从控制器传递到组件是不好的做法吗?任何意见,将不胜感激。非常感谢!

4

1 回答 1

3

Passing computed properties into a component from a controller is fine.

The code you have should be working. The one question/change is: Do you need to be returning a nested object in you controller's computed property? {property: this.get('someProperty')}`

If so, then you need to call .property on it in your component in order to access the controller's someProperty value.

my-component/template.hbs

{{myProperty.property}}

Here's an ember-twiddle demonstrating the code working.

于 2016-02-01T23:34:58.027 回答