使用ES6语法注册AngularJS服务

时间:2015-10-15 17:56:30

标签: javascript angularjs service ecmascript-6

我尝试使用ES6导入/导出语法创建和注册新服务,但我不确定正确的格式是什么。我使用它的唯一方法是如下,如果我在注入AccountService时在index.module.js中使用引号,它会中断。我应该在服务文件中创建新的services.account-service模块吗?当我不使用引号时,为什么只能正确注入服务?谢谢!

account.service.js:

class AccountService {

  exportTrack(response, account) {
      return {
          method: 'PUT',
          url: 'https://api.spotify.com/v1/me/tracks?ids=' +response.data.tracks.items[0].id+ '&type=track&market=US',
          headers: {
              'Authorization': 'Bearer ' + account.auth
          }
      };
  }
}

export default angular.module('services.account-service', [])
  .service('AccountService', AccountService)
  .name;

index.module.js:

import { config } from './index.config';
import { routerConfig } from './index.route';
import { runBlock } from './index.run';

import HeaderController from '../app/components/header/header.controller';
import SidenavController from '../app/components/sidenav/sidenav.controller';
import LoginController from './login/login.controller';
import AccountController from './account/account.controller';
import AccountService from './account/accountService-service';
import TrackController from './track/track.controller';
import PlaylistController from './playlist/playlist.controller';
import { Velociraptor } from '../app/components/services/velociraptor-service'; // Component Loopback service (lb-ng)

angular.module('velociraptor-ui',[
    'ngAnimate',
    'ngCookies',
    'ngTouch',
    'ngSanitize',
    'ngMessages',
    'ngAria',
    'ngResource',
    'ui.router',
    'ngMaterial',
    'Velociraptor',
    'toastr',
    AccountService
  ])
  .constant('moment', moment)
  .config(config)
  .config(routerConfig)
  .run(runBlock)
  .controller('HeaderController', HeaderController)
  .controller('SidenavController', SidenavController)
  .controller('LoginController', LoginController)
  .controller('AccountController', AccountController)
  .controller('TrackController', TrackController)
  .controller('PlaylistController', PlaylistController);

1 个答案:

答案 0 :(得分:1)

我的猜测是,在注入AccountService时,在我的index.module.js中使用引号'你的意思是你做了这样的事情:

services.account-service

这样它就不会起作用。导入的 Angular模块的名称仍然是AccountService(无论ES6模块是如何调用的),preg_match_all变量等于' services.account-service&#39 ;字符串。

我考虑将name property导出为模块化Angular工作流的实体模式,只需记住导出/导入的确切内容。