2

我正在尝试day.js在客户端加载两个插件。但它似乎没有工作......我做错了什么?

Pug / Jade文件

script(defer, src='javascript/external/day.js')
script(defer, src='javascript/external/day_minmax.js')
script(defer, src='javascript/external/day_isbetween.js')

script(defer, dayjs.extend(window.dayjs_plugin_minmax))
script(defer, dayjs.extend(window.dayjs_plugin_isbetween))

console输出

dayjs.max()
Uncaught TypeError: dayjs.max is not a function

加载的js插件文件来自:

https://unpkg.com/dayjs@1.9.1/plugin/isBetween.js

https://unpkg.com/dayjs@1.9.1/plugin/minMax.js

4

2 回答 2

4

这是 index.html 文件中的一个工作示例,您可以在根 Pug/Jade 文件中执行相同的操作。我也使用cdn版本,但您也可以从下载它们的文件夹中导入它们。

window.dayjs_plugin_minmaxvswindow.dayjs_plugin_minMax可能是问题

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Test page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
    <script src="https://unpkg.com/dayjs@1.8.21/plugin/isBetween.js"></script>
    <script src="https://unpkg.com/dayjs@1.8.21/plugin/minMax.js"></script>
  </head>
  <body></body>
  <script>
    const minMax = window.dayjs_plugin_minMax;
    const isBetween = window.dayjs_plugin_isBetween;
    dayjs.extend(minMax);
    dayjs.extend(isBetween);
    console.log(
      "MAX: ",
      dayjs.max(dayjs(), dayjs("2018-01-01"), dayjs("2019-01-01"))
    );
    console.log(
      "MIN: ",
      dayjs.min(dayjs(), dayjs("2018-01-01"), dayjs("2019-01-01"))
    );
    console.log(
      "BETWEEN: ",
      dayjs("2016-10-30").isBetween("2016-01-01", "2016-10-30", null, "[)")
    );
  </script>
</html>

这是结果:

结果图片

于 2020-10-12T14:14:27.843 回答
0

在官方网站上有一个“隐藏”部分,其中包含使用浏览器导入插件的示例。

https://day.js.org/docs/en/plugin/loading-into-browser

就像他们说的那样为我工作:

<script src="path/to/dayjs/plugin/advancedFormat"></script>
<!-- Load plugin as window.dayjs_plugin_NAME -->
<script>
  dayjs.extend(window.dayjs_plugin_advancedFormat)
</script>

于 2021-12-13T13:41:17.840 回答