Angular+MEAN Stack使用Highcharts报Cannot read properties of undefined (reading 'time')错误求助
解决Highcharts "Cannot read properties of undefined (reading 'time')" 错误
这个错误的核心原因是Highcharts初始化时缺少Time模块依赖——即便你没用到时间相关功能,Highcharts的部分基础组件也会默认依赖该模块。以下是具体修复方案:
1. 补全Highcharts模块导入
如果使用npm安装的Highcharts,在组件文件顶部补充Time模块的导入与初始化:
import * as Highcharts from 'highcharts'; import HighchartsTime from 'highcharts/modules/time'; // 初始化Time模块 HighchartsTime(Highcharts);
2. 确保数据合法且加载完成
- 验证
new_data的结构:new_data.email必须是字符串数组(如['user1@test.com', 'user2@test.com']),new_data.login必须是数字数组(如[3, 7, 12])。 - 若数据来自MEAN后端接口,需等待数据返回后再初始化
chartOptions,避免使用未加载的异步数据:
ngOnInit() { // 示例:通过服务获取后端数据 this.userService.getLoginActivity().subscribe((new_data) => { this.chartOptions = { chart: { type: 'line' }, title: { text: 'Top 5 User Login Activity Chart' }, xAxis: { categories: new_data.email, title: { text: 'Users' } }, yAxis: { title: { text: 'No of User Logins' } }, series: [{ type: 'line', name: 'No of time Loggedin', data: new_data.login }] }; }); }
3. 统一Highcharts相关包版本
版本不兼容也可能导致模块缺失问题,执行以下命令更新到匹配版本:
npm install highcharts@latest highcharts-angular@latest
内容的提问来源于stack exchange,提问作者themadhatter
相关产品推荐
相关产品推荐

