From b3152edbf2f45878683cfdc42bccdf2b643c375f Mon Sep 17 00:00:00 2001 From: somonus <312677182@qq.com> Date: Thu, 27 Oct 2016 14:14:35 +0800 Subject: [PATCH] fix the bug when option contains function --- src/components/Echarts/renderChart.js | 3 ++- src/util/toString.js | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/util/toString.js diff --git a/src/components/Echarts/renderChart.js b/src/components/Echarts/renderChart.js index 04f4cd9..e76b23a 100644 --- a/src/components/Echarts/renderChart.js +++ b/src/components/Echarts/renderChart.js @@ -1,4 +1,5 @@ import echarts from './echarts.min'; +import toString from '../../util/toString'; export default function renderChart(option) { return ` @@ -30,7 +31,7 @@ export default function renderChart(option) { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 使用刚指定的配置项和数据显示图表。 - myChart.setOption(${JSON.stringify(option)}); + myChart.setOption(${toString(option)}); diff --git a/src/util/toString.js b/src/util/toString.js new file mode 100644 index 0000000..ac379ba --- /dev/null +++ b/src/util/toString.js @@ -0,0 +1,8 @@ +export default function toString(obj) { + return JSON.stringify(obj, function(key, val) { + if (typeof val === 'function') { + return `~--demo--~${val}~--demo--~`; + } + return val; + }).replace('\"~--demo--~', '').replace('~--demo--~\"', '').replace(/\\n/g, ''); +}