今天引入ueditor遇到报错:Uncaught TypeError: Cannot read property ‘offsetWidth‘ of null

原因是dialog还没初始化完成,导致获取不到offsetWidth,解决方案如下:
<template>
<el-dialog
:title="!dataForm.id ? '新增' : '修改'"
top="5px"
:close-on-click-modal="false"
:visible.sync="visible">
<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<el-form-item label="标题" prop="title">
<el-input v-model="dataForm.title" placeholder="标题"></el-input>
</el-form-item>
<el-form-item label="内容" prop="content">
<script :id="ueId" class="ueditor-box" type="text/plain" style="width: 500px; height: 260px;"></script>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false">取消</el-button>
<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
</span>
</el-dialog>
</template>
<script>
import ueditor from 'ueditor'
export default {
data () {
return {
visible: false,
dataForm: {
id: 0,
title: '',
content: ''
},
dataRule: {
title: [
{ required: true, message: '标题不能为空', trigger: 'blur' }
]
},
ue: null,
ueId: `J_ueditorBox_${new Date().getTime()}`,
}
},
methods: {
//初始化
init (id) {
this.dataForm.id = id || 0
this.visible = true
this.$nextTick(() => {
this.$refs['dataForm'].resetFields()
//解决办法:等dialog渲染完成再获取editor
this.ue = ueditor.getEditor(this.ueId, {
// serverUrl: '', // 服务器统一请求接口路径
zIndex: 3000
})
if (this.dataForm.id) {
this.$http({
url: this.$http.adornUrl(`/sys/sysnotify/info/${this.dataForm.id}`),
method: 'get',
params: this.$http.adornParams()
}).then(({data}) => {
if (data && data.code === 0) {
this.dataForm.title = data.sysNotify.title
this.dataForm.content = data.sysNotify.content
//ueditor回显
ue.addListener('ready',function () {
ue.execCommand('insertHtml',data.sysNotify.content);
});
}
})
}
})
},
// 表单提交
dataFormSubmit () {
this.$refs['dataForm'].validate((valid) => {
if (valid) {
this.$http({
url: this.$http.adornUrl(`/sys/sysnotify/${!this.dataForm.id ? 'save' : 'update'}`),
method: 'post',
data: this.$http.adornData({
'id': this.dataForm.id || undefined,
'title': this.dataForm.title,
'content': this.ue.getContent()
})
}).then(({data}) => {
if (data && data.code === 0) {
this.$message({
message: '操作成功',
type: 'success',
duration: 1500,
onClose: () => {
this.visible = false
this.$emit('refreshDataList')
}
})
} else {
this.$message.error(data.msg)
}
})
}
})
}
}
}
</script>
ueditor是通过插件方式引入

《黄帝内经·素问》