富文本编辑器vue-quill-editor

一、install-->command line:

1、cnpm install vue-quill-editor --save

2、cnpm install sass-loader@7.3.1 --save-dev

3、cnpm install --save-dev node-sass

二、官网("https://www.awesomes.cn/repo/surmon-china/vue-quill-editor")-->Nuxt.js example code("https://github.com/surmon-china/surmon-china.github.io/tree/source/projects/vue-quill-editor/nuxt")

 

三、create (plugins/nuxt-quill-plugin.js)

四、configure (nuxt.config.js)

 五、copy and modify(vue file)-->editor.vue

 

 1 <template>
 2   <!--两列布局-->
 3   <div class="wrapper release-tc">
 4     <div class="release-box">
 5       <h3>发布吐槽</h3>
 6       <div class="editor">
 7         <quill-editor
 8           ref="myQuillEditor"
 9           v-model="content"
10           :options="editorOption"
11           @blur="onEditorBlur($event)"
12           @focus="onEditorFocus($event)"
13           @ready="onEditorReady($event)"
14         />
15         <div class="btns">
16           <button class="sui-btn btn-danger btn-release" @click="save">发布</button>
17         </div>
18         <div class="clearfix"></div>
19       </div>
20     </div>
21     <div class="clearfix"></div>
22   </div>
23 </template>
24 <script>
25 import "~/assets/css/page-sj-spit-submit.css"
26 import spitApi from '@/api/spit'
27 import {quillRedefine} from 'vue-quill-editor-upload'
28 export default {
29   name: "quill-example-nuxt",
30   data() {
31     return {
32       content: "",
33       editorOption: {}
34     };
35   },
36   computed: {
37       editor() {
38           return this.$refs.myQuillEditor.quill;
39       },
40   },
41   created(){
42     this.editorOption=quillRedefine({
43       uploadConfig:{
44         action: 'http://localhost:3000/upload',
45         res: (response)=>{
46           return response.info
47         },
48         name: 'img'
49       }
50     })
51   },
52   mounted() {
53     console.log(
54       "App inited, the Quill instance object is:",
55       this.$refs.myQuillEditor.quill
56     );
57     // setTimeout(() => {
58     //   this.content = "I was changed!";
59     // }, 3000);
60   },
61   methods: {
62     onEditorBlur(editor) {
63       console.log("editor blur!", editor);
64     },
65     onEditorFocus(editor) {
66       console.log("editor focus!", editor);
67     },
68     onEditorReady(editor) {
69       console.log("editor ready!", editor);
70     },
71     onEditorChange({ editor, html, text }) {
72         console.log('editor change!', editor, html, text)
73         this.content = html
74       },
75     save(){
76       spitApi.save({content:this.content}).then(res=>{
77         this.$message({
78           message:res.data.message,
79           type:(res.data.flag?'success':'error')
80         })
81         if(res.data.flag){
82           this.$router.push('/spit')
83         }
84       })
85     }
86   }
87 };
88 </script>
89 <style lang="scss" scoped>
90     .quill-editor {
91       height: 50px;
92       min-height: 200px;
93       max-height: 400px;
94       overflow-y: auto;
95     }
96 </style>

 

 六、auxiliary add-ins:vue-quill-editor-upload

1、install-->commandline:cnpm install vue-quill-editor-upload --save

2、import into vue-->("    import {quillRedefine} from 'vue-quill-editor-upload'      ")

3、use

 1 data() {
 2     return {
 3       content: "",
 4       editorOption: {}
 5     };
 6   },
 7 created(){
 8     this.editorOption=quillRedefine({
 9       uploadConfig:{
10         action: '',
11         res: (response)=>{
12           return response.info
13         },
14         name: 'img'
15       }
16     })
17   },

 七、multer、Express

1、use "upload-server"

2、install-->commandline:cnpm install

3、run -->npm run start

 4、docking with the program(vue)

 5、correspond to multer(server.js)

 1 const express = require('express');
 2 const upload = require('multer')({ dest: 'uploads/' });
 3 const path = require('path');
 4 const fs = require('fs');
 5 const port = 3000;
 6 
 7 let  app = express();
 8 
 9 app.set('port', port);
10 app.use(express.static(path.join(__dirname, 'uploads/')));
11 
12 //处理跨域请求
13 app.all('*',function (req, res, next) {
14   res.header('Access-Control-Allow-Origin', '*');
15   next();
16 });
17 
18 app.post('/upload', upload.single('img'), (req, res) => {
19   // 没有附带文件
20   if (!req.file) {
21     res.json({ ok: false });
22     return;
23   }
24      
25   // 输出文件信息
26  
27   console.log('====================================================');
28   console.log('fieldname: ' + req.file.fieldname);
29   console.log('originalname: ' + req.file.originalname);
30   console.log('encoding: ' + req.file.encoding);
31   console.log('mimetype: ' + req.file.mimetype);
32   console.log('size: ' + (req.file.size / 1024).toFixed(2) + 'KB');
33   console.log('destination: ' + req.file.destination);
34   console.log('filename: ' + req.file.filename);
35   console.log('path: ' + req.file.path);
36   
37   
38   // 重命名文件 
39  
40   let oldPath = path.join(__dirname, req.file.path);
41   let newPath = path.join(__dirname, 'uploads/' + req.file.originalname);
42   fs.rename(oldPath, newPath, (err) => {
43     if (err) {
44       res.json({ ok: false });
45       console.log(err);
46     } else {
47       res.json({ ok: true ,info:'http://localhost:'+port+'/'+req.file.originalname});
48     }
49   });
50   
51 });
52 
53 app.listen(port, () => {
54   console.log("[Server] localhost:" + port);
55 });

 6、effect:

 

posted @ 2020-07-26 21:18  遥~  阅读(347)  评论(0编辑  收藏  举报