今天来公司,修补上周富文本编辑器无法在项目中现实的问题,其实问题比较简单,由于富文本编辑器是独立插件,所以就想在html中内联js进行初始化,按照wangEditor的说明,在html页面尾部添加了初始化代码。
<script type="text/javascript">
const E = window.wangEditor
const editor1 = new E('#wangEditor1')
var menus = ['head', 'bold', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough',
'indent', 'lineHeight', 'foreColor', 'backColor', 'link', 'list', 'justify',
'quote', 'image', 'table', 'code', 'splitLine', 'undo', 'redo',
]
editor1.config.menus = menus
editor1.config.uploadImgServer = '/iPDMessage/saveImg'
editor1.config.uploadFileName = 'FileName'
editor1.config.uploadImgMaxSize = 20 * 1024 * 1024;//设置图片大小为20M
editor1.config.uploadImgTimeout = 1000000; //图片上传超时限制10s
editor1.create()
</script>
但是在页面中,没有产生富文本编辑器,查看页面元素可以发现,wangEditor1元素中为空,这是由于项目使用了vue框架,浏览器不会执行内部js代码,因此将初始化代码放置在created函数中即可。
created:function (){
const E = window.wangEditor
const editor1 = new E('#wangEditor1')
var menus = ['head', 'bold', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough',
'indent', 'lineHeight', 'foreColor', 'backColor', 'link', 'list', 'justify',
'quote', 'image', 'table', 'code', 'splitLine', 'undo', 'redo',
]
editor1.config.menus = menus
editor1.config.uploadImgServer = '/iPDMessage/saveImg'
editor1.config.uploadFileName = 'FileName'
editor1.config.uploadImgMaxSize = 20 * 1024 * 1024;//设置图片大小为20M
editor1.config.uploadImgTimeout = 1000000; //图片上传超时限制10s
editor1.create()
},
尝试使用$(document).ready()来主动激活初始化代码,但是也没有调用成功,感觉是vue的生命周期复写了jQuery的生命周期函数,导致调用失败,这个问题还在研究,待发现问题根源在更新。