Angular.js DI с классами (ES6) и наследованием
Фон, текущая реализация классов/модулей в нашем приложении - это классы common.js и CoffeeScript. Я отчаянно ищу решение для работы с ES6 или TypeScript, но проблема остается.
Как сделать DI с наследованием класса с помощью Angular -1.x?
С учетом кода:
// SuperService.js
class SuperService {
constructor($http, $q, $etc) {
// Implementation is not important ...
}
}
export { SubService }
// SubService.js
import { SuperService } from './SuperService';
class SubService extends SuperService {
constructor($more, $di, $things, $here) {
// Implementation is not important ...
// // // // // // // // // //
// Problem exists here ... //
// // // // // // // // // //
super($we, $need, $all, $the, $super, \
$class, $di, $things, $every, $time, $we, \
$inherit, $from, $it)
}
}
export { SubService }
Должно ли одно в SubService
переопределить все исходные требования DI для успешного вызова super()
?
В настоящее время мы делаем что-то похожее на следующее:
// CoffeeScript // Javascript
app.factory "subService", (Service) -> app.factory("subService", function(Service) {
SubService = () -> var SubService;
Service.call(@) SubService = function() {
@ Service.call(this);
return this;
# Overwrite some stuff on the "Service" };
Service::basepath = "/environments" Service.prototype.basepath = "/environments";
Service::model = Environment Service.prototype.model = Environment;
return new SubService();
new SubService() });
Который также менее идеален, кроме того, что он уродлив.
Ответы
Ответ 1
это не ES6 (это ES7), но он просто может плавать на вашей лодке
Я рассмотрел angular-decorators MikeRyan52.
Он собрал декоратор @inject
, который работает следующим образом:
@inject('$http', '$rootScope')
class SomeClass {
constructor($http, $rootScope) {
}
}
@inject('$q')
class SubClass extends SomeClass {
constructor($q, ...parentDependencies) { /** parent dependencies caught with a rest parameter **/
super(...parentDependencies);
}
}
реализация @inject
Ответ 2
добавьте это в самое дно SubService.js
SubService.$inject = ['$http', '$q', '$etc', '$more', '$di', '$things', '$here'];