2016-02-03 35 views
0

我想用pikadaymoment,但不知何故我不能使它工作。pikaday與時刻不起作用

我想在一個aureliacustom-attribute如下使用它:

import "moment"; 
import "pikaday"; 

... 
    self.picker = new Pikaday({ 
      field: $(this.element)[0] 
      , format: "DD.MM.YYYY" 
      , onSelect: (date) => { 
       console.log(self.picker.getMoment().format('Do MMMM YYYY')); //getMoment() is always null. 
      } 
     }); 

儘管進口moment之前pikaday,它不工作(在其他SO問題,並在GitHub的問題決定),並getMoment( )始終爲空。即使日期不是按照format的默認格式。

我還發現,hasMoment總是falsepikaday.js,因爲它不是在一個代碼塊正確設置(在pikaday源如可以在下面SSEN):

(function (root, factory) 
{ 
    'use strict'; 

    var moment; 
    if (typeof exports === 'object') { 
     // CommonJS module 
     // Load moment.js as an optional dependency 
     try { moment = require('moment'); } catch (e) {} 
     module.exports = factory(moment); 
    } else if (typeof define === 'function' && define.amd) { 
     // AMD. Register as an anonymous module. 
     define(function (req) 
     { 
      // Load moment.js as an optional dependency 
      var id = 'moment'; 
      try { moment = req(id); } catch (e) {} 
      return factory(moment); 
     }); 
    } else { 
     root.Pikaday = factory(root.moment); 
    } 
}(this, function (moment) 
{ 
    'use strict'; 

    /** 
    * feature detection and helper functions 
    */ 
    var hasMoment = typeof moment === 'function', // this is always false as it can't be set in the above block. 
..... 

這讓我認爲我可能會以錯誤的方式導入moment。任何人都可以幫助我嗎?

其他細節: 我的項目設置如下:

tsconfig.json

{ 
    "compilerOptions": { 
     "target": "es6", 
     "isolatedModules": false, 
     "jsx": "react", 
     "declaration": false, 
     "noImplicitAny": false, 
     "removeComments": true, 
     "noLib": false, 
     "preserveConstEnums": true, 
     "suppressImplicitAnyIndexErrors": false, 
     "experimentalDecorators": true, 
     "emitDecoratorMetadata": true, 
     "experimentalAsyncFunctions": true, 
     "allowSyntheticDefaultImports": false   
    }, 
    "filesGlob": [ 
     "**/*.ts", 
     "**/*.tsx", 
     "!node_modules/**" 
    ], 
    "files": [ ], 
    "compileOnSave": false, 
    "buildOnSave": false, 
    "atom": { 
     "rewriteTsconfig": false 
    } 
} 

我也使用system.js

System.config({ 
    baseURL: "/", 
    defaultJSExtensions: true, 
    transpiler: "babel", 
    babelOptions: { 
    "optional": [ 
     "runtime", 
     "optimisation.modules.system" 
    ] 
    }, 
    paths: { 
    "*": "dist/*", 
    "github:*": "jspm_packages/github/*", 
    "npm:*": "jspm_packages/npm/*" 
    }, 
    map: { 
     ... 
    } 
}) 

解決方法:內部main(Aurelia路上配置):

import moment from "moment"; 
... 
    // Register moment on global scope. 
    window["moment"] = moment; 
... 

而且,是的,我知道這是醜陋的,但做這項工作:)

+0

你有沒有安裝jspm的時刻? 'jspm install npm:moment',實際上如果你不使用它,你不需要導入'時刻'。如果您需要它'從'時刻'輸入時刻 – valichek

+0

謝謝您的回覆。實際上,我試着從'時刻'和'輸入'時刻都嘗試'輸入時刻',但是他們都沒有達到我的目的。至於使用它,「時刻」並沒有被明確地用在我的腳本代碼中。不過,我正在使用日曆控件'pikaday',它會查找「時刻」並嘗試使用它;因此,我需要以某種方式加載它。 ** P.S。**雖然''瞬間'的輸入時刻有助於直接在我的打字稿文件中使用。 –

回答

1

這可能是在這一點上有很大幫助你沒有,但是這是怎麼了我解決它:

import * as moment from 'moment'; 
import * as Pikaday from 'pikaday'; 

Pikaday.prototype.toString = function (format: string) { 
    return (this._d instanceof Date) ? moment(this._d).format(format || this._o.format) : ''; 
}; 
0

不知道如何系統JS的作品,但你可能必須以某種方式確保它得到pikaday之前加載,並暴露在其他模塊作爲全球 - 如jQuery,並承諾polyfils通常處理。

相關問題