在angular中混合javascript和typescript时出错
我正在尝试在我的angular应用程序中使用typescript。 我有一个描述类用户的ts文件:在angular中混合javascript和typescript时出错,javascript,angularjs,typescript,ecmascript-6,Javascript,Angularjs,Typescript,Ecmascript 6,我正在尝试在我的angular应用程序中使用typescript。 我有一个描述类用户的ts文件: module App.Tools { export class User { name: string; constructor(name: string) { this.name = name; } } } import {User} from 'User.ts'; export default cla
module App.Tools {
export class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
}
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;
}
}
}
在我的JS文件(es2015)中,我尝试导入类用户:
module App.Tools {
export class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
}
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;
}
}
}
这不起作用,因为我得到一个错误:“用户未定义”
我正在将gulp与browserify、tsify和babel一起使用。我认为您需要“User.ts”文件中的“”类型脚本模块:
更新1
使用内部模块:
module App.Tools {
export class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
}
export = App.Tools;
要在导入中使用此选项,请执行以下操作:
import { User } from 'User.ts';
.ts
扩展不属于import
语句。您好,谢谢您的回复。不幸的是,您的解决方案不起作用。行'this.currentUser=new User(authData.google.displayName,authData.provider);'似乎无法工作更新:我发现使用nodejs无法使用内部模块。必须使用外部模块。