在 angular 中混合 javascript 和打字稿时出错

Error when mixing javascript and typescript in angular

本文关键字:出错 angular 混合 javascript      更新时间:2023-09-26

我正在尝试在我的部分角度应用程序中使用打字稿。我有一个描述类用户的ts文件:

module App.Tools {
    export class User {
        name: string;
        constructor(name: string) {
            this.name = name;
        }
    }
}

在我的JS文件(es2015)中,我尝试导入类User:

import  {User} from 'User.ts';
export default class AuthFactory {
    /**
     * @param  {[type]}
     * @return {[type]}
     */
    constructor($firebaseAuth) {
        this.ref = new Firebase("...");
        // create an instance of the authentication service
        this.auth = $firebaseAuth(this.ref);
        var authData = this.auth.$getAuth();
        if (authData) {
            this.currentUser = new User(authData.google.displayName, authData.provider);
        } else {
            this.currentUser = null;
        }
    }
}

这不起作用,因为我收到错误:"未定义用户"。

我正在使用带有浏览器化,tsify和babel的gulp。

我认为您需要"User.ts"文件中的"外部"打字稿模块:

class User {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
}
export = User;

更新 1

使用内部模块:

module App.Tools {
    export class User {
        name: string;
        constructor(name: string) {
            this.name = name;
        }
    }
}
export = App.Tools;

要在导入中使用此功能:

import { User } from 'User.ts';