在React

时间:2018-12-13 10:40:53

标签: reactjs chart.js momentjs

我正在尝试在React应用程序中设置Chart.js的语言环境。

方法1

Use the bundled Chart.js版本,因为我在其余的应用程序中未使用moment

import Chart from 'chart.js/dist/Chart.bundle.min.js';

问题:如何设置语言环境? moment不能作为全局对象使用,也不能作为Chart或其任何实例的属性来找到,也没有用于修改语言环境的API。

方法2

Use the non bundled版本,包括moment我自己。

import * as moment from 'moment';
import 'moment/locale/es';
import Chart from 'chart.js/dist/Chart.min.js';

然后在呈现图表之前输入代码:

moment.locale('es');
chart = new Chart(type, options);

问题:,但这确实会引发错误:

Error: Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com

我尝试将moment分配为window.moment = moment到本地对象,但是有相同的错误。

(方法)Hack 3

为了测试,我将moment.jses.js文件复制到public文件夹中,并将它们作为<script>标记包含在index.html中,并且可以工作,但是我没有不想这样的黑客。

注释

我知道一些Chart.js的React库,但不想使用它们。

我读过另一篇文章,他们建议如果在呈现语言环境之前更改了语言环境,则建议重新加载图表。

[https://stackoverflow.com/questions/38202587/chart-js-moment-locale][https://stackoverflow.com/questions/38202587/chart-js-moment-locale]

https://github.com/apertureless/vue-chartjs/issues/101

https://github.com/chartjs/Chart.js/issues/5284

https://github.com/chartjs/Chart.js/issues/2906

1 个答案:

答案 0 :(得分:0)

在 Vue 中我尝试过。

import Moment from 'moment';
import 'moment/locale/es';
import Chart from 'chart.js';
Moment.locale('es');

它对我有用。

我正在查看我看到的 chart.js 代码蚂蚁,他们将 moment 称为 Moment 而不是 moment