2016-01-11 105 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財產,並相應地處理你的依賴注入。