2016-01-11 104 views
1

我是Angular内部ES6语法的新手。我试图创建一个服务并使用$httpLocalStorageModule将模块注入服务AngularJS ES6

当我尝试将两个模块注入到我的服务中时,我得到有关我的语法没有处于严格模式的错误。我究竟做错了什么?

服务/ auth.js

export default class { 

    constructor($http, localStorageService) { 
    this.$http = $http; 
    this.localStorageService = localStorageService; 
    } 
} 

服务/ index.js

import angular from 'angular'; 

import auth from './auth'; 

export default angular 
    .module('app.services', []) 
    .service('Authentication', auth) 
    .name; 

采用了棱角分明1.4.5

+0

是否错误消失,如果你能为你的模块严格模式? –

回答

4

我能够通过使用'ngInject';来解决它在构造函数中。

我认为这是行不通的,因为当'ngInject'让巴贝尔transpiler知道如何处理这种注射

export default class { 

    constructor($http, localStorageService) { 
    'ngInject'; 
    this.$http = $http; 
    this.localStorageService = localStorageService; 
    } 
} 
2

要正确实现这一目标,不依靠transpilers,只是一个静态的吸气剂添加到您的课,像这...

export class { 
    constructor($http, localStorageService) { 
    // Store references to the $http and localStorageService providers 
    this.$http = $http; 
    this.localStorageService = localStorageService; 
    } 

    static get $inject() { 
    return ['$http', 'localStorageService']; 
    } 
} 

AngularJS将确认在给定类$inject财产,并相应地处理你的依赖注入。