将一项服务注入另一项服务并不适用于ES6课程

时间:2017-08-10 12:45:51

标签: angularjs angularjs-service

当服务O(1)被实例化时,我得到以下错误:

  

Transition Rejection($ id:0 type:6,message:转换错误,详细信息:ReferenceError:BooksListService未定义)

我的代码如下。它是在ES6中编写的,并由Babelify for Browserify转换为ES5。

出了什么问题?为什么注射不能在UserBookService构造函数中工作?

据我所知,只要在服务构造函数中将其名称声明为参数,就可以将服务注入到另一个服务中。它适用于UserBooksService常量,但不适用于该服务。注入userBooksDefinition在路由定义的解析( user-books.route.js )中也可以正常工作。

用户books.service.js

BoooksListService

用户books.module.js

// UserBooksService service definition.

export default class UserBooksService {
  constructor(BooksListService, userBooksDefinition) {
    this.userBooksDefinition = userBooksDefinition;
    this.BooksListService = BooksListService;
  }

  // Get books of given user.
  getUserBooks(user) {
    let userBooksIds = this.userBooksDefinition;
    let userBooksObjects = {};

    // Converts list of books IDs to list of books objects.
    for (var bookIdsListName in userBooksIds) {
      if (userBooksIds.hasOwnProperty(bookIdsListName)) {
        // Fetch and save single list of books.
        let idsList = userBooksIds[bookIdsListName];
        userBooksObjects[bookIdsListName] = BooksListService.getBooks(idsList);
      }
    }

    return userBooksObjects;
  }
}

用户books.route.js

// userBooks module.

import angular from 'angular';
import uiRouter from '@uirouter/angularjs';

import UserBooksService from './user-books.service';
import userBooksComponent from './user-books.component';
import userBooksDefinition from './user-books-definition';
import userBooksRoute from './user-books.route';


export default angular
  .module('app.components.userBooks', [
    uiRouter
  ])
  .config(userBooksRoute)
  .service('UserBooksService', UserBooksService)
  .constant('userBooksDefinition', userBooksDefinition)
  .component('userBooks', userBooksComponent)
  .name;

1 个答案:

答案 0 :(得分:2)

BookListService现在是UserBooksService的属性。您必须使用thisthis.BookListService

访问它