wangEditor上传图片基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
/* 生产的html标签默认不带样式,需要自己自定义,如果想要默认样式可以参考官网,如下 */
<link rel="stylesheet" href="https://www.wangeditor.com/demo/css/view.css">
</head>
<style>
#editor—wrapper {
border: 1px solid #ccc;
z-index: 100;
}
#toolbar-container { border-bottom: 1px solid #ccc; }
#editor-container { height: 500px; }
</style>
<body>
/* 回显html标签需要用此div包裹 */
<div id="editor-content-view" class="editor-content-view">
<blockquote>此标签在编辑器里为 “引用”样式,原生html不能解析,需自定义样式。</blockquote>
</div>
<div id="editor—wrapper">
<div id="toolbar-container"></div>
<div id="editor-container"></div>
</div>
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
<script>
const { createEditor, createToolbar } = window.wangEditor
const editorConfig = {
placeholder: '输入内容...',
onChange(editor) {
const html = editor.getHtml()
},
MENU_CONF: {
uploadImage: {}
}
}
editorConfig.MENU_CONF['uploadImage'] = {
fieldName: 'img',
server: 'http://localhost/upload',
base64LimitSize: 5 * 1024 * 1024,
async customInsert(res, insertFn) {
console.log(res);
const url = res.data.url
insertFn(url)
},
headers: {
token: 'eyJhbGciOiJIUzI1NiJ...'
},
}
editorConfig.MENU_CONF['codeSelectLang'] = {
codeLangs: [
{ text: 'CSS', value: 'css' },
{ text: 'HTML', value: 'html' },
{ text: 'XML', value: 'xml' },
{ text: 'JAVA', value: 'java' },
]
}
const editor = createEditor({
selector: '#editor-container',
html: "",
config: editorConfig,
mode: 'default',
})
const toolbarConfig = {}
const toolbar = createToolbar({
editor,
selector: '#toolbar-container',
config: toolbarConfig,
mode: 'default',
})
</script>
</body>
</html>
详情见官网文档