如何允许组件中的函数访问在构造函数中导入的工厂?

时间:2017-01-23 21:34:47

标签: angularjs meteor components angular-meteor

我一直在尝试在组件之间共享数据,并遵循here的建议来建立商店。

这一切都在我的组件构造函数中运行良好,但我无法弄清楚如何赋予函数类似的访问权限。

class Home {
  constructor($scope, $reactive, Store) {
    'ngInject';

    $reactive(this).attach($scope);

    this.selectedDate = Store.get().selectedDate;
}

这一切都有效,但访问此处的商店并不起作用:

nextDay(){
    'ngInject';
    Store.set({selectedDate: moment(this.selectedDate).add(1, 'd').format('YYYY-MM-DD')});
    console.log('nextDay');
}

我已经尝试将Store附加到$ reactive,我已经尝试了这个。存储并将Store作为agrument传递给nextDay(),但无法弄明白。任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

您应该在Class

上分配服务(注入的东西)

例如,如果您想使用Store assign this.Store = Store 然后你可以在类中的所有方法中使用它作为this.Store

  class Home {
      constructor($scope, $reactive, Store) {
        'ngInject';
        this.Store = Store;
        this.$reactive = $reactive;
        this.$scope = $scope;
        this.$reactive(this).attach($scope);

        this.selectedDate = this.Store.get().selectedDate;
      }

      nextDay(){
        this.Store.set({selectedDate: moment(this.selectedDate).add(1, 'd').format('YYYY-MM-DD')});
        console.log('nextDay');

      }
  }