当服务O(1)
被实例化时,我得到以下错误:
Transition Rejection($ id:0 type:6,message:转换错误,详细信息:ReferenceError:BooksListService未定义)
我的代码如下。它是在ES6中编写的,并由Babelify for Browserify转换为ES5。
出了什么问题?为什么注射不能在UserBookService
构造函数中工作?
据我所知,只要在服务构造函数中将其名称声明为参数,就可以将服务注入到另一个服务中。它适用于UserBooksService
常量,但不适用于该服务。注入userBooksDefinition
在路由定义的解析( user-books.route.js )中也可以正常工作。
BoooksListService
// 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;
}
}
// 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;
答案 0 :(得分:2)
BookListService
现在是UserBooksService
的属性。您必须使用this
:this.BookListService