2017-02-22 6 views
-1

У меня есть класс AbstractRepository и класс UserRepository extends AbstractRepository. Я объявляю super() в моем конструкторе, но ошибка _http не определена. Как я могу вставить $ http в класс UserRepository.constructor super() es6 angularjs

AbstractRepository:

import _ from 'lodash'; 
import 'rxjs/add/operator/map'; 

class AbstractRepository { 
    _http; 
    _config; 
    _Model; 

    constructor($http, config, Model) { 
     'NgInject' 
     this._http = $http; 
     this._config = config; 
     this._Model = Model; 
    } 

    /** 
    * Gets all instances of repository entities 
    * 
    * @returns {Observable<Model[]>} - List of observables of all entities 
    */ 
    getAll() { 
     const url = this._config.url; 
     return this._http.get(url) 
      .map(dataList => dataList.json()) 
      .map(dataList => _.map(dataList, item => new this._Model(item))); 
    } 

    /** 
    * Gets a specific item of repository entity 
    * 
    * @param {number} itemId - ID of an item to find 
    * @returns {Observable<Model>} - Observable of repo entity item 
    */ 
    getItem(itemId) { 
     const url = this._config.url + itemId; 
     return this._http.get(url) 
      .map(item => item.json()) 
      .map(item => new this._Model(item)); 
    } 

    /** 
    * Creates a specific item of repository entity 
    * 
    * @param {Model} item - Item to create 
    * @returns {Observable<Model>} - Observable of recently created item 
    */ 
    createItem(item) { 
     const url = this._config.url; 
     return this._http.post(url, item) 
      .map(item => item.json()) 
      .map(item => new this._Model(item)); 
    } 

    /** 
    * Updates a specific item of repository entity 
    * 
    * @param {number} itemId - ID of an item to update 
    * @param {Model} item - Wanted updated item 
    * @returns {Observable<Model>} - Observable of repo entity item 
    */ 
    updateItem(itemId, item) { 
     const url = this._config.url + itemId; 
     return this._http.put(url, item) 
      .map(item => item.json()) 
      .map(item => new this._Model(item)); 
    } 
} 

export {AbstractRepository} 

UserRepository:

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 { 
    _http = $http; 
    constructor() { 
     '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}; 
+0

Что такое '_http = $ http;'? Это неверно. И откуда, по вашему мнению, возникает «$ http»? – Bergi

+0

Я хочу установить стандартный angularjs $ http для моего свойства класса –

+0

Случайная строка «NgInject» cringeworthy –

ответ

0

Вы все еще должны объявить $http в качестве параметра вашего ребенка конструктора, если вы хотите, чтобы направить его в родительский конструктор:

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

Спасибо, что это работает! –

Смежные вопросы