我正在尝试使用pikaday
with moment
,但不知何故我无法使其工作。
我正在尝试aurelia
custom-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
总是false
in pikaday.js
,因为它在第一个代码块中没有正确设置(在下面的pikaday
as 的源代码中):
(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;
...
而且,是的,我知道这很丑陋,但确实有效:)