展示
前提:
后台返回的是一段绘制用的js脚本,并是以父向子传参的方法传递过来,无法以正常的import方式使用echarts
解决:
methods:{ introduce(){ var script1 = document.createElement("script"); script1.src = "http://www.chinaops.cn/cloud/home/js/01/jquery-1.4.2.min.js"; document.head.append(script1); } }, created(){ this.introduce(); }
|
data(){ return{ script:null } }, props:{ scr:"", }, methods:{ introduce(){ this.script = document.createElement("script"); document.body.append(this.script); } }, computed(){ draw(){ this.script.innerHTML = this.scr; } }
|
<template> <div class="resImg"> {{draw}} <div id="factors_image" name="factors_image" :style="{width: '300px', height: '300px'}"></div> </div> </template>
|
get请求.then(res => { this.json = res.data.factorJson; var str = res.data.graphicsScript; str = this.insertStr(str, 0, "var factors =" + this.json + ";"); this.scr = str; })
insertStr(soure, start, newStr) { return soure.slice(0, start) + newStr + soure.slice(start); }
|
factorJson的数据
graphicsScript的数据