【问题标题】:Error when mixing javascript and typescript in angular以角度混合javascript和打字稿时出错
【发布时间】:2016-05-02 22:11:22
【问题描述】:

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

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

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

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;
        }
    }
}

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

我正在使用带有 browserify、tsify 和 babel 的 gulp。

【问题讨论】:

  • .ts 扩展不属于import 语句。

标签: javascript angularjs typescript ecmascript-6


【解决方案1】:

我认为您需要在“User.ts”文件中添加一个“external”打字稿模块:

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';

【讨论】:

  • 您好,感谢您的回复。不幸的是,您的解决方案不起作用。行 'this.currentUser = new User(authData.google.displayName, authData.provider);'好像不行
  • 更新:我发现使用 nodejs 是不可能使用内部模块的。必须坚持使用外部模块。
猜你喜欢
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2017-05-29
  • 2016-02-03
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
相关资源
最近更新 更多