Lee*_*ley 14 javascript dependency-injection angularjs ecmascript-6
背景,我们的应用程序中的类/模块的当前实现是common.js和CoffeeScript类.我正在拼命寻找使用ES6或TypeScript的解决方案,但问题仍然存在.
如何使用Angular-1.x进行类继承的DI?
鉴于代码:
// 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 }
Run Code Online (Sandbox Code Playgroud)
必须一个,在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() });
Run Code Online (Sandbox Code Playgroud)
除了丑陋之外,这也不太理想.
小智 2
它不是 ES6(它是 ES7),但它可能会让你的船漂浮
我会研究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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3569 次 |
最近记录: |