问题描述
我意识到,这个问题重复了,但上一次没有提供恰当的答案时刻包已经安装
I realize, this question is repeated but previous once does not provide apt answermoment package was already installed
1.安装包
npm install moment-timezone --save
在node_modules目录内
|
|--时刻
|--moment-timezone
Inside node_modules directory
|
|--moment
|--moment-timezone
存在的目录
2.Index.html 包含脚本
<script src="node_modules/moment-timezone/moment-timezone.js"></script>
System.config.js
var map = {
'moment': 'node_modules/moment',
'momentzone': 'node_modules/moment-timezone'
};
var packages = {
'moment': { defaultExtension: 'js' },
'momentzone': { defaultExtension: 'js' }
};
3.Inside component.ts 文件
import * as moment from 'moment/moment';
export class TimeComponent implements OnInit{
ngOninit(){
console.log(moment("2014-06-01T12:00:00Z").tz('America/Los_Angeles').format('ha z'));
}
}
应该导入什么来防止出错时刻"类型不存在属性 tz
What should be imported to prevent errorProperty tz does not exist on type 'Moment'
推荐答案
这可能会有所帮助.
为 angular-cli 或 npm install 运行以下命令.
Run Following command for angular-cli or npm install.
sudo npm install moment moment-timezone --save
npm install @types/moment @types/moment-timezone --save-dev
对于 npm systemjs.config.js
for npm systemjs.config.js
map: {
'moment': 'npm:moment',
'moment-timezone': 'npm:moment-timezone/builds'
}
packages: {
'moment': {
main: './moment.js',
defaultExtension: 'js'
},
'moment-timezone': {
main: './moment-timezone-with-data-2010-2020.min.js',
defaultExtension: 'js'
}
}
你想在 .ts 文件中使用时区的地方
where ever you want to use time-zone in .ts file
import * as moment from 'moment-timezone';
@Component({
selector: 'my-app',
template: `<h1>Hello {{name}}</h1>`,
})
export class AppComponent {
name = 'Angular';
jun = moment();// creating obj.
constructor() {
this.jun.tz('America/Los_Angeles').format('hh : mm : ss a z');
console.log(this.jun.tz('America/Los_Angeles').format('hh : mm : ss a z'));
console.log(moment.tz.names()); // for all time zone.
}
这篇关于如何在 Angular 2 App 中包含时刻时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!