收到错误moment.moment是不是一个函数,使用茉莉花和Moment.js

问题描述:

我尝试茉莉花与moment.js一起选择,但我得到这个错误...收到错误moment.moment是不是一个函数,使用茉莉花和Moment.js

debug.js:21 Uncaught TypeError: (0 , _moment.moment) is not a function 

不知道如果它与moment.js相关,或者如果我设置不正确。感谢任何帮助。

//script.js 
 

 
import { moment } from 'moment'; 
 

 
export class Age { 
 
    
 
    constructor(age, secondDate) { 
 
    this.age = age; 
 
    this.secondDate = secondDate; 
 
    } 
 

 
    getSecondsBetweenTwoDates(age, secondDate){ 
 
    age = moment(this.age).format('l'); 
 
    secondDate = moment(this.secondDate).format('l'); 
 
    //differenceInSeconds = moment((this.secondDate).diff(this.age, 'days')); 
 
    differenceInDays = age.diff(secondDate, 'days'); 
 
    //let differenceInDays = this.age - this.secondDate 
 
    return differenceInDays; 
 

 
    } 
 
} 
 

 

 
//age-spec.js 
 

 
import { Age } from './../js/age.js'; 
 

 
describe('Age', function() { 
 
    
 
    let reusableDate, 
 
     today, 
 
     testDate = '2016-10-05', 
 
     date = '2016-10-10'; 
 

 
    beforeEach(() => { 
 
    reusableDate = new Age(date, testDate); 
 
    console.log(reusableDate); 
 
    const mockedDateAndTime = '2017-03-02 00:00:00'; 
 
    today = moment(mockedDateAndTime).toDate(); 
 
    console.log('this is today', today); 
 
    jasmine.clock().mockDate(today); 
 
    }); 
 

 
    it('should return the difference between today',() => { 
 
    console.log(date); 
 
    console.log(testDate); 
 
    console.log(reusableDate.getSecondsBetweenTwoDates(date, testDate)); 
 
    console.log(typeof(reusableDate.getSecondsBetweenTwoDates)); 
 
    //expect(5).toEqual(reusableDate.getSecondsBetweenTwoDates()); 
 
    }); 
 

 

 
});

我没有使用beforeEach块可言,那只是东西,我在谷歌找到,并试图...我还安装了卡玛时刻插件像这样:

框架:[ '的jquery-3.2.1', '茉莉', 'browserify', '瞬间-2.9.0'],

插件:[ '果报的jquery', “karma- browserify', '卡玛时刻', '果报茉莉花', '因果报应 - 铬 - 发射', “因果报应 - 茉莉HTML-记者 ]

moment包中没有moment命名导出。大多数NPM软件包是CommonJS/UMD,主要导出为module.exports。即使moment被写入as ES module with default export,它也可以作为UMD模块导入,因为它有different entry points for different environments

应该

import * as moment from 'moment'; 

import moment from 'moment'; 

的选择取决于项目配置。通常,* as反映了真正的CommonJS出口更好,并且如果在捆绑过程中将项目配置为回退到CJS模块,则不太可能导致问题。

+0

感谢您的回答,我不知道这一点。 – Lucky500

我犯了一个愚蠢的错误,由于某种原因,我ES6文件顶部的导入语法不起作用。

我在script.js文件中对此进行了更改: 从'moment'导入{moment};

to this: var moment = require('moment');

+0

由于非常具体的原因,它不起作用。因为没有名为“出口”的“时刻”。 – estus