简体   繁体   English

在ES6中将纯类注入angular 1.x应用程序的正确方法

[英]Correct way to inject pure class into angular 1.x application in ES6

A colleague claims this is an incorrect way to inject a pure ES6 JavaScript class into Angular. 一位同事声称这是向Angular注入纯ES6 JavaScript类的错误方法。 I am curious if there is a better way (more correct)? 我很好奇是否有更好的方法(更正确)?

As an aside, is it better (and why is it better) to attach the injected dependencies ( $timeout in this example) to the instance; 顺便说一句,将注入的依赖项(在此示例中为$timeout )附加到实例是否更好(以及为什么更好); eg, this._$timeout = $timeout in the constructor. 例如,构造函数中的this._$timeout = $timeout I personally think there is no advantage to doing that in this case. 我个人认为在这种情况下做这件事没有任何好处。

class.factory.js class.factory.js

let ClassFactory = function($timeout) {
    // Factory function that simply returns class constructor.

    class MyClass {
        constructor(a, b) {
            // contrived class
            this.a = a;
            this.b = b;
            this.c = null;
        }

        applyChange() {
            // contrived class method
            const SELF = this;

            $timeout(() => {
                SELF.c = SELF.a + SELF.b;
            });
        }
    }

    return MyClass ;
};

ClassFactory.$inject = ['$timeout'];

export default ClassFactory;

app.module.js app.module.js

import ClassFactory from './factories/class.factory';
import AppService from './services/app.service';


export default angular.module('myApp', [])
    .factory('ClassFactory', ClassFactory)
    .service('AppService', AppService);

Later, elsewhere we may use the class in some service or controller, to construct new MyClass instances. 稍后,在其他地方我们可以在某些服务或控制器中使用该类来构造新的MyClass实例。

app.service.js app.service.js

class AppService {
    // contrived usage of our dependency injected pure class.

    constructor(ClassFactory) {
        this.array = [];
        this._ClassFactory = ClassFactory;
    }

    initialize(a, b) {
        // We can instantiate as many "MyClass" objects as we need.
        let myClass = new this._ClassFactory(a, b);

        this.array.push(myClass);
    }

    static serviceFactory(...injected) {
        AppService.instance = new AppService(...injected);
        return AppService.instance;
    }
}

AppService.serviceFactory.$inject = ['ClassFactory'];

export default AppService.serviceFactory;

At this point it doesn't matter if $timeout is class property or just local variable. 在这一点上, $timeout是类属性还是只是局部变量并不重要。

Wrapping a class with factory function doesn't play well with ES6 development , it's not possible to export and extend it. 包含具有工厂功能的类在ES6开发中不能很好地进行,因此无法导出和扩展它。 The fact that a factory is needed probably indicates design problem. 需要工厂的事实可能表明设计问题。

A class like that can get the dependencies via dependency injection (in common sense ). 像这样的类可以通过依赖注入获得依赖( 通常意义上 )。 This is a usual thing when class constructor is supposed to be called with non-dependency arguments too: 当类构造函数也应该使用非依赖性参数调用时,这是常见的事情:

export class MyClass {
  constructor($timeout, a, b) {
    this._$timeout = $timeout;
    ...
  }
}
...
obj = new MyClass($timeout, a, b);

If there's more than one dependency, $injector dependency can be provided instead of all dependencies: 如果有多个依赖项,则可以提供$injector依赖项而不是所有依赖项:

export class MyClass {
  constructor($injector, a, b) {
    this._$timeout = $injector.get('$timeout');
    ...
  }
}
...
obj = new MyClass($injector, a, b);

There may also be a design problem that causes the dependency on $timeout , and by solving it the dependency could be avoided. 可能还存在导致依赖于$timeout的设计问题,并且通过解决它可以避免依赖性。 It's not clear from the code above why MyClass should trigger a digest with $timeout , its logic doesn't contain anything that would require that. 从上面的代码中不清楚为什么MyClass应该用$timeout触发摘要,它的逻辑不包含任何需要它的东西。 It's the responsibility of code that uses MyClass instance and binds it to view or whatever a digest is there for. 使用MyClass实例并将其绑定到视图或任何摘要的代码是代码的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM