【问题标题】:Computed property don't observe changes in objects in array计算属性不观察数组中对象的变化
【发布时间】:2017-03-24 22:52:02
【问题描述】:

当我更改语言时,我的组件不会观察到我的对象数组的变化。它仅在更改路线后才有效,但值得立即更改所有内容,无需重新加载页面。

组件代码:

  columnsToEdit: Ember.computed('i18n.locale', function() {
    let i18n = this.get('i18n');
    return Ember.A([
      {
        "propertyName": "name",
        "title": i18n.t('Name')
      },
      {
        "propertyName": "phone",
        "title": i18n.t('Name'),        
      },
    ]);
  }),

  columns: Ember.computed.map('columnsToEdit.[]', function(column){
    return column;
  }),

我认为,它应该有效。 “ColumnsToEdit”在“columsToEdit”中查找来自 i18n.locale 的更改。下一个“columns”查找“columnsToEdit”中的更改。当我在“列”中调用“console.log”时,它会返回预期的结果,但在我看来,单击“语言切换”后什么都没有发生。那么为什么我的组件没有改变视图中的“标题”呢?任何帮助将不胜感激。

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    显示更多代码。 Your code is working fine!.

    但是,您需要了解您的属性columns 将始终与columnsToEdit 完全相同。我不确定您想要做什么,但目前您在columns 中没有做任何事情。

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      currentLocale: Ember.computed.readOnly('i18n.locale'),
      columnsToEdit: Ember.computed('currentLocale', function() {
          let i18n = this.get('i18n');
          return Ember.A([
            {
              "propertyName": "name",
              "title": i18n.t('Name')
            },
            {
              "propertyName": "phone",
              "title": i18n.t('Name'),        
            },
          ]);
        }),
      
        columns: Ember.computed.map('columnsToEdit.[]', function(column){
          return column;
        }),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-07
        • 2017-04-29
        • 2019-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-25
        相关资源
        最近更新 更多