【发布时间】:2018-12-13 10:40:52
【问题描述】:
我正在尝试在 React 应用程序中设置 Chart.js 的语言环境。
方法一
Use the bundledChart.js 版本,因为我在应用程序的其余部分不使用moment:
import Chart from 'chart.js/dist/Chart.bundle.min.js';
问题:如何设置语言环境? moment 不能作为全局对象使用,我也找不到它作为 Chart 或其任何实例的属性,也没有用于修改语言环境的 API。
方法二
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.js 和es.js 文件复制到public 文件夹,并将它们作为<script> 标签包含在index.html 中,它有效,但我不想要这样的黑客攻击。
注意事项
我知道一些用于 Chart.js 的 React 库,但不想使用它们。
我读过其他帖子,如果在渲染之前更改了区域设置,他们建议重新加载图表。
https://github.com/apertureless/vue-chartjs/issues/101
【问题讨论】: