【问题标题】:is there a way to use dependencies injection using typescript有没有办法使用打字稿使用依赖注入
【发布时间】:2017-07-31 00:56:36
【问题描述】:

我对此比较陌生,所以我想使用 typescript 实现依赖注入(这是我第一次使用这种模式),我更多的是使用 java 或 c# 等语言编程来进行 OOP,所以还有更多易于应用这种模式, 我在互联网上找到了一个示例,我可以在 eclipse 和 Visual Studio 上毫无问题地使用它,但是当我在 typescript 上使用它时,IDE 会引发如下错误:

Supplied parameters do not match any signature of call target

当出现此错误时,它只是在执行结束时

我的基类:

class Motor {
    Acelerar(): void {
    }
    GetRevoluciones(): number {
        let currentRPM: number = 0;
        return currentRPM;
    }
}
export {Motor};

我的班级使用电机

import { Motor } from "./1";
class Vehiculo {
    private m: Motor;
    public Vehiculo(motorVehiculo: Motor) {
        this.m = motorVehiculo;
    }
    public GetRevolucionesMotor(): number {
        if (this.m != null) {
            return this.m.GetRevoluciones();
        }
        else {
            return -1;
        }
    }
}
export { Vehiculo };

我的界面和电机类型

interface IMotor {
    Acelerar(): void;
    GetRevoluciones(): number;
}
class MotorGasoline implements IMotor {
    private DoAdmission() { }
    private DoCompression() { }
    private DoExplosion() { }
    private DoEscape() { }
    Acelerar() {
        this.DoAdmission();
        this.DoCompression();
        this.DoExplosion();
        this.DoEscape();
    }
    GetRevoluciones() {
        let currentRPM: number = 0;
        return currentRPM;
    }
}
class MotorDiesel implements IMotor {
    Acelerar() {
        this.DoAdmission();
        this.DoCompression();
        this.DoCombustion();
        this.DoEscape();
    }
    GetRevoluciones() {
        let currentRPM: number = 0;
        return currentRPM;
    }
    DoAdmission() { }
    DoCompression() { }
    DoCombustion() { }
    DoEscape() { }
}

这是出现错误的地方:

import { Vehiculo } from "./2";
enum TypeMotor {
    MOTOR_GASOLINE = 0,
    MOTOR_DIESEL = 1
}
class VehiculoFactory {
    public static VehiculoCreate(tipo: TypeMotor) {
        let v: Vehiculo = null;
        switch (tipo) {
            case TypeMotor.MOTOR_DIESEL:
                v = new Vehiculo(new MotorDiesel()); break;
            case TypeMotor.MOTOR_GASOLINE:
                v = new Vehiculo(new MotorGasoline()); break;
            default: break;
        }
        return v;
    }
}

我暂时不想使用任何库或模块,如 SIMPLE-DIJS 或 D4js 或任何其他,我只想知道如何在没有它们的情况下实现

【问题讨论】:

    标签: dependency-injection typescript2.0


    【解决方案1】:

    您遇到此错误是因为您没有在 Vehiculo 类型上指定构造函数。

    要声明构造函数,您应该使用constructor 关键字而不是类的名称。

    class Vehiculo {
        private m: Motor;
        constructor(motorVehiculo: Motor) {
            this.m = motorVehiculo;
        }
        public GetRevolucionesMotor(): number {
            if (this.m != null) {
                return this.m.GetRevoluciones();
            }
            else {
                return -1;
            }
        }
    }
    

    【讨论】:

    • 这行得通,谢谢,但你知道如何将参数放入类工厂,假设“motor gas”或“motorelectric”有它自己的构造函数
    • 我不明白。您能否用代码示例更新您的问题以及您想做什么?
    猜你喜欢
    • 2020-03-15
    • 2016-01-06
    • 2021-04-27
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2017-01-06
    相关资源
    最近更新 更多