【问题标题】:Setting an Angular service on an ES6 class property affects its functionality在 ES6 类属性上设置 Angular 服务会影响其功能
【发布时间】:2016-08-22 08:47:26
【问题描述】:

我正在按照本指南http://angular-tips.com/blog/2015/06/using-angular-1-dot-x-with-es6-and-webpack/ 使用带有 Angular 的 ES6。它建议将服务定义为类。在服务定义中,我正在注入 $cookieStore 服务并将其附加到类:

export default class Authentication {
  constructor($cookieStore) {
    this.$cookieStore = $cookieStore;
  }

  setAuthenticatedAccount(account) {
    this.$cookieStore.put(JSON.stringify(account), 'authenticatedAccount');
  }
}

当调用setAuthenticatedAccount 时,this.$cookieStore 会执行但不会创建 cookie authenticatedAccount。如果您调用 $cookieStore 而不将其附加到 this,则会创建 cookie。

如何在避免这个问题的同时将服务定义为类?

【问题讨论】:

    标签: javascript angularjs ecmascript-6


    【解决方案1】:

    您以错误的方式调用put 函数。试试这个参数顺序:

    this.$cookieStore.put('authenticatedAccount', JSON.stringify(account));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2016-10-17
      • 2014-04-08
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      相关资源
      最近更新 更多