ES6 的类中使用单例模式
核心代码:
export default class ChartUtil {
static getInstace() {
if (!ChartUtil.instace) {
ChartUtil.instace = new ChartUtil();
console.log('新创建了一个实例');
return ChartUtil.instace;
}
console.log('复用之前的实例');
return ChartUtil.instace;
}
constructor() {
this.initChartLabel();
}
initChartLabel() {
this.lineChartStartLabel = '<objectiveHistory>';
}
}
创建实例:
this.chartUtils = ChartUtils.getInstace();
效果图:
还没有评论,来说两句吧...