Typescript 奥雷利亚CLI&;打字稿&;动量
我不会让Aurelia(CLI)、TypeScript和MomentJS一起工作。我已经看到了Aurelia&Moment问题的解决方案,但它们没有使用Aurelia CLI 以下是我目前正在做的事情: 使用Aurelia CLI的新Aurelia项目:Typescript 奥雷利亚CLI&;打字稿&;动量,typescript,momentjs,aurelia,Typescript,Momentjs,Aurelia,我不会让Aurelia(CLI)、TypeScript和MomentJS一起工作。我已经看到了Aurelia&Moment问题的解决方案,但它们没有使用Aurelia CLI 以下是我目前正在做的事情: 使用Aurelia CLI的新Aurelia项目: au new import * as moment from 'moment'; export class App { message = 'Hello World!'; constructor(){ }
au new
import * as moment from 'moment';
export class App {
message = 'Hello World!';
constructor(){
}
hello() : string {
return moment().format();
}
}
我选择TypeScript而不是Babel
安装力矩
npm install moment --save
这将安装Moment2.4.1。我可以从node_模块中找到它(包括moment.d.ts)
编辑aurelia.json
我将“时刻”添加到“依赖项”:
在app.ts中使用瞬间
当我现在尝试在app.ts中导入矩时,问题开始出现
import { moment } from 'moment';
这会产生错误:“模块”o:/dev/spikes/amment/node_modules/moment/moment”没有导出的成员“moment”
更改外壳可修复此错误:
import { Moment } from 'moment';
但在这一点上,我完全被卡住了。当尝试使用“时刻”时,我总是会出现错误“找不到‘时刻’的名称”。以下是当前的app.ts,它给出了“找不到名称‘时刻’”-错误:
进口似乎是个问题。有什么办法可以解决这个问题吗
更新 在将app.ts修复为如下所示之后,现在就可以编译了。但它在运行时给出“TypeError:无法读取未定义的属性'format'
import { Moment } from 'moment';
import { autoinject } from "aurelia-framework";
export class App {
message: string;
moment: Moment;
constructor(moment: Moment) {
this.moment = moment;
this.message = this.moment.format('MMMM Do YYYY, h:mm:ss a')
}
}
更新
根据上一个错误,如果没有@autoinject,autoinject似乎无法工作。因此,添加了这一点,错误发生了变化:“TypeError:moment.format不是函数”
更新 这实际上不起作用。不是编译的东西,但在运行应用程序时,我得到了“TypeError:无法读取未定义的属性'format' 原创 啊,我真傻。我只需要在课堂上注入一点时间。很明显 以下是工作版本:
import { Moment } from 'moment';
import { autoinject } from "aurelia-framework";
export class App {
message = 'Hello World!';
moment: Moment;
constructor(moment : Moment){
this.moment = moment;
}
hello() : string {
return this.moment.format();
}
}
MomentJS是一个全局模块,它只导出
moment
变量。
d.ts文件中的其他接口定义,例如,接口力矩
,将不会真正导出。这些是用于TypeScript编译器和intellisense的
这就是为什么您会遇到上面提到的TS编译器和类型错误问题。您可能用autoinject欺骗了编译器,但没有欺骗浏览器
时刻:
要使其正常工作,请使用如下导入语句:
import*作为“时刻”的时刻代码>
之后,矩
变量变为可用,您可以像平常一样使用它
在课堂上的用法:
au new
import * as moment from 'moment';
export class App {
message = 'Hello World!';
constructor(){
}
hello() : string {
return moment().format();
}
}
你能详细解释一下为什么从“时刻”开始导入*作为时刻吗代码>是必需的,而aurelia docs使用从“时刻”导入时刻代码>
import * as moment from 'moment';
export class App {
message = 'Hello World!';
constructor(){
}
hello() : string {
return moment().format();
}
}