使用vue-element-admin遇到的问题!
computed
方法传参:采用闭包进行传参
router
利用addRoutes添加路由后,页面跳转需要跳转到通用的路由,才能看到动态加载的路由!
tinymce 富文本编辑器的封装(tinymce版本为5.2.2)
有注册或购买过服务的话,直接通过组件配置api-key直接使用,我没有注册购买,而且比较急,所以直接利用下载的方式使用了,这样的话,就需要做一层封装了。
安装tinymce-vue
npm install @tinymce/tinymce-vue -S
下载tinymce
npm install tinymce -S
将node_modules文件中的tinymce的skins文件复制下来,放到public文件中,我这里建一个tinymce为文件夹进行存放,方便管理
下载语言包
可以在tinymce官网下载中文语言包下载链接。然后将下载好的语言包文件放到public文件的tinymce中。
封装的前期准备到这里就结束了,接下来就是组件封装了。
组件封装
考虑到组件可能会在大多数场景进行使用的问题,所以将组件封装到全局components文件中
导入tinymce组件
导入tinymce组件和主题
在组件模板中使用tinymce组件
export default {
components: { Editor },
name: 'Tinymce',
props: {
id: {
type: String,
default: function() {
return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
}
},
value: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
},
toolbar: {
type: Array,
default() {
return ['bold italic underline alignleft aligncenter alignright']
}
},
plugins:{
type: Array,
default() {
return ['advlist anchor autolink autosave code']
}
}
height: {
type: [Number, String],
required: false,
default: 360
},
width: {
type: [Number, String],
required: false,
default: 'auto'
}
},
data() {
return {
content: this.value,
init: {
selector: `#${this.tinymceId}`,
skin_url: 'tinymce/skins/ui/oxide', // 一定要指定一套ui风格
language_url: 'tinymce/langs/zh_CN.js',
language: 'zh_CN',
height: this.height,
plugins: this.plugins, // 插件
toolbar: this.toolbar, // 工具栏
branding: false,
images_upload_handler: (blobInfo, success, failure) => {
this.handleImgUpload(blobInfo, success, failure)
}
}
}
},
mounted() {
/* 如果在这里传入上面的 init 对象,并不能生效,
但什么参数都不传也会报错,所以这里传入一个空对象 */
tinymce.init({})
},
watch: {
value(newValue) {
this.content = newValue
},
content(newValue) {
this.$emit('getValue', newValue)
}
},
methods: {
//自定义图片上传方式
handleImgUpload(blobInfo, success, failure) {
const imageData = new FormData()
// formdata.set('upload_file', blobInfo.blob())
// axios.post('/api/upload', formdata).then(res => {
// success(res.data.data.src)
// }).catch(res => {
// failure('error')
// })
}
}
}
可能出现的问题
语言包路径问题
使用封装好的tinymce组件后,可能会出现图片显示的错误提示,这个是init参数里面的路径有错误造成的,建议先将language_url和language先删除再试
tinymce显示问题
使用tinymce后,组件使用并没有出现错误警告,但是界面上显示是空白的,那么就有可能是skin_url的路径错误了,需要检查一下路径是否正确,因为下载的版本不一样,文件里面的样式路径也会存在不同。