2017-02-22 87 views
1

我有1個服務可以擴展其他服務。當我嘗試在控制器中導入它時得到了這個錯誤ng:areq fn不是獲得UserRepository的函數。我認爲我的問題是正確的注入這項服務。這裏是我的服務:ng:areq fn不是函數objectService angularjs es6

import { USER_REPO_CONFIG } from '../repository.config'; 
import { AbstractRepository } from '../abstract.repository'; 
import { UserModel } from './user.model'; 

import 'rxjs/add/operator/map'; 

class UserRepository extends AbstractRepository { 
    constructor($http) { 
     'NgInject' 
     super($http, USER_REPO_CONFIG, UserModel); 
    } 

    /** 
    * Gets the user by ID 
    * 
    * @param {number} userId - ID of a user to find 
    * @returns {Observable<UserModel>} - User model instance 
    */ 
    getUser(userId) { 
     return this.getItem(userId); 
    } 

    /** 
    * Creates the user 
    * 
    * @param {UserModel} user - User model instance 
    * @returns {Observable<UserModel>} - User model instance observable 
    */ 
    createUser(user) { 
     return this.createItem(user); 
    } 

    /** 
    * Updates the user 
    * 
    * @param {number} userId - ID of a user to update 
    * @param {UserModel} user - User model instance 
    * @returns {Observable<UserModel>} - User model instance observable 
    */ 
    updateUser(userId, user) { 
     return this.updateItem(userId, user); 
    } 
} 

export { UserRepository }; 

這裏是我的模塊:

import { HelloComponent } from './hello/hello.component'; 
import { UserRepository } from '../core/repository/user/user.repository'; 

const greetingModule = angular 
        .module('app.greeting',[]) 
        .component('helloComponent', new HelloComponent) 
        .service('UserRepository', new UserRepository) 
        .name; 

export { greetingModule }; 

和我的控制器:

import { UserRepository } from '../../core/repository/user/user.repository'; 

class HelloController { 
    constructor(UserRepository) { 
     this.hello = 'Hello World'; 
     this.service = UserRepository; 
    } 

    getAll() { 
     console.log('user Repository', this.service) 
    } 
} 
HelloController.$inject = ['UserRepository']; 
export { HelloController }; 

回答

0
.service('UserRepository', new UserRepository) 

​​需要一個功能,你傳遞一個對象。您可以直接通過該類:

.service('UserRepository', UserRepository) 

傳遞類有效地傳遞構造函數。

相關問題