Moment JS导入无法与Angular一起正常工作

时间:2018-07-01 06:27:34

标签: angular momentjs moment-range

我有这样的问题。我将在其中构建一个角度应用程序,我将使用矩距和矩距js。

我已将其导入需要的组件上。这就是component.ts文件。它给我这样的错误。

import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroup, FormGroupDirective, NgForm, Validators} from '@angular/forms';
import {ErrorStateMatcher} from '@angular/material/core';
import {ReservationService} from "../shared/reservation.service";
import {Router} from "@angular/router";
import {Reservation} from "../shared/reservation";
import * as $ from 'jquery';
import { BootstrapAlertService, BootstrapAlert } from 'ngx-bootstrap-alert';


import Moment from 'moment';
import { extendMoment } from 'moment-range';

const moment = extendMoment(Moment);



export interface Time {
  value: string;
  viewValue: string;
}

export interface Year {
  value: string;
  viewValue: string;
}

export interface Role {
  value: string;
  viewValue: string;
}



export class MyErrorStateMatcher implements ErrorStateMatcher {
  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    const isSubmitted = form && form.submitted;
    return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
  }
}

@Component({
  selector: 'app-makereservation',
  templateUrl: './makereservation.component.html',
  styleUrls: ['./makereservation.component.css'],
  providers: [ReservationService]
})
export class MakereservationComponent implements OnInit {

  public show = false;
  public reservation:Reservation=new Reservation();
  date:string;
  lab:string;
  from:string;
  to:string;
  name:string;
  email:string;
  role:string;
  year:string;
  reason:string;

  time: Time[] = [
    {value: '8.00', viewValue: '8.00'},
    {value: '9.00', viewValue: '9.00'},
    {value: '10.00', viewValue: '10.00'},
    {value: '11.00', viewValue: '11.00'},
    {value: '12.00', viewValue: '12.00'},
    {value: '13.00', viewValue: '13.00'},
    {value: '14.00', viewValue: '14.00'},
    {value: '15.00', viewValue: '15.00'},
    {value: '16.00', viewValue: '16.00'},
    {value: '17.00', viewValue: '17.00'}

  ];

  years: Year[] = [
    {value: '1st Year', viewValue: '1st Year'},
    {value: '2nd Year', viewValue: '2nd Year'},
    {value: '3rd Year', viewValue: '3rd Year'},
    {value: '4th Year', viewValue: '4th Year'},
  ];

  roles: Role[] = [
    {value: 'Student', viewValue: 'Student'},
    {value: 'Lecturer', viewValue: 'Lecturer'},
    {value: 'Professor', viewValue: 'Professor'},
    {value: 'Assistant lecturer', viewValue: 'Assistant lecturer'},
    {value: 'Instructor', viewValue: 'Instructor'},
  ];



  constructor(private router: Router, private reservationService: ReservationService,private bootstrapAlertService:BootstrapAlertService) { }

  ngOnInit() {

  }

  emailFormControl = new FormControl('', [
    Validators.required,
    Validators.email,
  ]);

  matcher = new MyErrorStateMatcher();

  getcurrentReservations(){
    if(this.lab && this.date) {
      this.reservation.date = this.date;
      this.reservation.lab = this.lab;
      this.reservationService.getcurrentReservations(this.reservation).subscribe((res: any) => {
        this.reservationService.reservations = res as Reservation[];

      });
    }
  }

  showYear(){
    if(this.role=="Student"){
      $( "#year" ).show();
    }
  }

  makereservation(){
    this.reservation.date=this.date;
    this.reservation.from=this.from;
    this.reservation.to=this.to;
    this.reservation.lab=this.lab;
    this.reservation.name=this.name;
    this.reservation.email=this.email;
    this.reservation.role=this.role;
    this.reservation.year=this.year;
    this.reservation.reason=this.reason;
    this.reservationService.makereservation(this.reservation).subscribe((res:any)=>{
      this.bootstrapAlertService.alert(new BootstrapAlert("Succesfully add a reservation!", "alert-success"));
      this.router.navigate(['/makereservation']);
    })

  }

}

这是浏览器控制台中显示的错误。

Uncaught TypeError: Cannot set property 'range' of undefined
    at Object.u [as extendMoment] (moment-range.js:1)
    at eval (makereservation.component.ts:14)
    at Object../src/app/makereservation/makereservation.component.ts (main.bundle.js:278)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (app.routing.ts:13)
    at Object../src/app/app.routing.ts (main.bundle.js:212)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (app.module.ts:3)
    at Object../src/app/app.module.ts (main.bundle.js:204)
    at __webpack_require__ (inline.bundle.js:55)

在webStrom中,它向我显示一个错误,并在import语句中加了Moment字样,并指出node_modules / moment / moment中没有默认导入。

有人可以帮助我找到解决此问题的方法吗?谢谢!

2 个答案:

答案 0 :(得分:3)

这可能是Angular应用程序的修复程序:

import * as moment from 'moment';
import { extendMoment } from 'moment-range';

const { moment } = extendMoment(moment);

仍在使用:

const range1 = moment.range('2018-01-20', '2018-01-27');

答案 1 :(得分:0)

这可能是针对Angular 7 + 应用程序的修复:

import * as moment from 'moment';
import { extendMoment } from 'moment-range';

const { range } = extendMoment(moment);

仍在使用:

const range1 = range('2018-01-20', '2018-01-27');