Typescript如何在构造函数中从其他类访问方法?

时间:2017-12-10 16:52:00

标签: typescript dependency-injection

我有2个课程UserLogicUserModel我想在UserModel构建器中访问UserLogic的方法...我该怎么做?

import { UserModel, ItUser } from '../../models/user.model';

export class UserLogic {

    public user: ItUser;

    constructor() {
        //UserModel is singleton class
        this.user = UserModel.model().user;
        console.log(this.user);
    }
}

1 个答案:

答案 0 :(得分:0)

  

我想在UserLogic构造函数中访问UserModel的方法......我该怎么做

根据您的代码,您可以访问UserModel的实例。所以只需调用方法,例如foo

import { UserModel, ItUser } from '../../models/user.model';

export class UserLogic {

    public user: ItUser;

    constructor() {
        this.user = UserModel.model().user;


        // Just call the method. eg.
        this.user.foo(); 
    }
}