【问题标题】:Angular 8: Using two external angular packages in same component - moment.jsAngular 8:在同一组件中使用两个外部 Angular 包 - moment.js
【发布时间】:2020-03-11 10:51:53
【问题描述】:

需要使用外部包Moment.js(记录正常的当前日期)和另一个外部包moment-feiertage(用于德国公众假期)。

我的问题是,Moment.jsmoment-feiertage 都在使用 moment 来实现它们的功能。

我怎样才能在同一个组件中同时使用import * as moment from 'moment';import * as moment from 'moment-feiertage';

我的examples.component.ts 包含以下代码:

import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';
//import * as moment from 'moment-feiertage';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
})
export class ExampleComponent implements OnInit {

  public today: string;
  public todayIsHoliday: boolean = false;
  constructor() { }

  ngOnInit() {
    this.today = moment().format('DD-MM-YYYY');
    console.log('today: ', this.today);

    //this.todayIsHoliday = moment().isHoliday();
    //console.log('todayIsHoliday: ', this.todayIsHoliday);
  }
}

【问题讨论】:

  • 你不能使用同一个名字两次,这几乎就是别名的原因。你为什么不简单地为feiertage 使用不同的别名?而且很可能你只需要导入第二个。
  • 来自文档:moment-feiertage 导出具有扩展功能的原始 moment 对象。

标签: angular typescript momentjs


【解决方案1】:

正如docs 的第一部分所述,moment-feiertage 导出具有扩展功能的原始矩对象,因此您只需 import * as moment from 'moment-feiertage 并使用它而不是原始矩。

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多