【发布时间】:2019-01-10 17:57:52
【问题描述】:
我正在尝试将日期时间功能与需要 moment.js 库的 Tabulator 一起使用。在我添加 Tabulator 之前的应用程序中,moment.js 已经在该级别的某些组件中使用。我有一个使用 Tabulator 并尝试使用 datetime 的新测试组件。通常我只会导入时刻并在这里使用它,但似乎在制表符本身中需要时刻。
我的第一个想法是 Moment.js 需要在我的应用程序中全局设置,所以我这样做了。
Main.js:
```
import Vue from 'vue'
import App from './App'
import router from './router'
import { store } from './store'
import Vuetify from 'vuetify'
import 'vuetify/dist/vuetify.min.css'
import moment from 'moment'
Vue.prototype.moment = moment
...............
new Vue({
el: '#app',
data () {
return {
info: null,
loading: true,
errored: false // this.$root.$data.errored
}
},
router,
components: { App },
template: '<App/>',
store
})
```
在我的组件中(Testpage.vue)
```
<template>
<div>
<div ref="example_table"></div>
</div>
</template>
<script>
// import moment from 'moment'
var Tabulator = require('tabulator-tables')
export default {
name: 'Test',
data: function () {
return {
tabulator: null, // variable to hold your table
tableData: [{id: 1, date: '2019-01-10'}] // data for table to display
}
},
watch: {
// update table if data changes
tableData: {
handler: function (newData) {
this.tabulator.replaceData(newData)
},
deep: true
}
},
mounted () {
// instantiate Tabulator when element is mounted
this.tabulator = new Tabulator(this.$refs.example_table, {
data: this.tableData, // link data to table
columns: [
{title: 'Date', field: 'date', formatter: 'datetime', formatterParams: {inputFormat: 'YYYY-MM-DD', outputFormat: 'DD/MM/YY', invalidPlaceholder: '(invalid date)'}}
],
}
</script>
```
我收到错误:“未捕获(承诺中)参考错误:在 Format.datetime (tabulator.js?ab1f:14619) 中未定义时刻” 我可以通过使用 this.$moment() 在其他组件中使用矩,但我需要它在 node_modules\tabulator-tables\dist\js\tabulator.js 中可用 因为这就是发生错误的地方。知道如何包含该库吗?
【问题讨论】:
-
完全不相关,但我会避免
moment变得一团糟,并用 date-fns 替换它,它为大小的一部分提供所有相同的功能,并提供可以成为的不变性以后很重要。
标签: vue.js vuejs2 vue-component tabulator