使用 Tabulator 5.4,并尝试允许用户对包含日期的列进行排序,文档中告诉我
需要依赖项您将需要包含 luxon.js 库以 使用这个分类器
但文档没有解释如何包含 luxon.js 库。
我已经安装了 luxon 使用
npm install luxon
我有
import {TabulatorFull as Tabulator} from 'tabulator-tables'
import { DateTime } from "luxon"
然后,在表定义中:
{title: 'Modified', field: 'modifiedTime', sorter:"date", cssClass: 'grey'},
但是当我运行这个时,我得到:
datetime.js:3 Uncaught ReferenceError: luxon is not defined
at Sort.datetime$2 (datetime.js:3:30)
at Sort.date$1 (date.js:9:18)
at Sort._sortRow (Sort.js:462:37)
at Sort.js:434:19
at Array.sort (<anonymous>)
at Sort._sortItems (Sort.js:428:8)
at Sort.sort (Sort.js:369:10)
at RowManager.refreshPipelines (RowManager.js:723:40)
at VirtualDomVertical.rerenderRows (VirtualDomVertical.js:98:4)
at RowManager.reRenderInPosition (RowManager.js:849:18)
我认为我没有将 luxon 安装在正确的位置或以正确的方式。我该怎么办?
正如Timur所建议的,这会起作用:
import {DateTime} from 'luxon'
window.DateTime = DateTime
根据 @Nigel 和 @Timur 的建议,请注意,如果您使用 Typescript,添加这两行将无法编译,并出现错误
Property 'DateTime' does not exist on type 'Window & typeof globalThis'
。要解决此问题,您需要创建一个类型文件(例如 src/types/index.d.ts
)并向其中添加以下内容:
export {}
declare global {
interface Window {
DateTime: any
}
}