简体   繁体   English

将Javascript和Typescript混合在angular中时出错

[英]Error when mixing javascript and typescript in angular

I am trying to use typescript in part of my angular application. 我试图在我的角度应用程序中使用打字稿。 I have a ts file describing a class User: 我有一个描述类User的ts文件:

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

And in my JS file (es2015) I try to import the class User: 在我的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;
        }
    }
}

This doesn't work as I get an error : "User is not defined". 由于我收到错误消息:“未定义用户”,此方法不起作用。

I am using gulp with browserify, tsify and babel. 我正在使用带有浏览器,tsify和babel的gulp。

I think you need an " external " typescript module in "User.ts" file: 我认为您需要在“ User.ts”文件中使用“ 外部 ”打字稿模块:

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

export = User;

Update 1 更新1

Using internal modules: 使用内部模块:

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

export = App.Tools;

To use this in import: 要在导入中使用它:

import { User } from 'User.ts';

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

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