2017-10-14 46 views
1

我嘗試茉莉花與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-記者 ]

回答

1

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

0

我犯了一個愚蠢的錯誤,由於某種原因,我ES6文件頂部的導入語法不起作用。

我在script.js文件中對此進行了更改: 從'moment'導入{moment};

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

+0

由於非常具體的原因,它不起作用。因爲沒有名爲「出口」的「時刻」。 – estus

相關問題