vue中关于img src写入本地地址自动加入http://localhost:8080的解决方法

用img标签 src写入本地地址时会自动在前面加入http://localhost:8080

<img src="E:/xxxxx/xxxx/xxxxx.jpg">

用网页F12查看:
在这里插入图片描述
解决方案为:

<img :src="require('E:/xxxxx/xxxx/xxxxx.jpg')">

然后图片就可以显示咯

以下是实现的步骤: 1.前端Vue中使用MavonEditor组件,并监听它的图片上传事件: ```vue <template> <div> <mavon-editor @imgAdd="handleImgAdd"></mavon-editor> </div> </template> <script> export default { methods: { handleImgAdd(imgFile, insertImgCallback) { //图片文件上传到后端 const formData = new FormData() formData.append('image', imgFile) axios.post('/api/upload', formData).then(response => { // 获取后端返回的图片URL const imgUrl = response.data.url // 在编辑器中插入图片 insertImgCallback(imgUrl) }) } } } </script> ``` 2. 在后端Express中定义一个上传图片的API路由,使用`multer`中间件来处理图片上传,并将图片路径返回给前端: ```javascript const express = require('express') const multer = require('multer') const path = require('path') const app = express() // 配置multer中间件,用于处理图片上传 const upload = multer({ storage: multer.diskStorage({ destination: function(req, file, cb) { cb(null, './uploads/') }, filename: function(req, file, cb) { const extname = path.extname(file.originalname) const filename = file.fieldname + '-' + Date.now() + extname cb(null, filename) } }) }) // 定义图片上传API路由 app.post('/api/upload', upload.single('image'), (req, res) => { const imageUrl = '/uploads/' + req.file.filename res.json({ url: imageUrl }) }) // 启动服务器 app.listen(3000, () => { console.log('Server started at http://localhost:3000') }) ``` 3.前端Vue中处理图片回显的逻辑,将后端返回的图片URL插入到编辑器中: ```vue <template> <div> <mavon-editor ref="editor"></mavon-editor> </div> </template> <script> export default { mounted() { // 从后端获取已保存的文章内容 axios.get('/api/article').then(response => { const articleContent = response.data.content // 将文章内容插入到编辑器中 this.$refs.editor.setContent(articleContent) }) } } </script> ``` 这样就完成了在Vue中使用MavonEditor组件上传图片并将图片保存到Express后端的流程,同时也实现了在Vue中回显已保存的文章内容中的图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值