elementUI + vue-quill-editor富文本編輯器實(shí)現(xiàn)圖片上傳服務(wù)器

這里遇到過一個(gè)坑森篷,頁(yè)面內(nèi)使用兩個(gè)富文本編輯器酝陈,v-model綁定同個(gè)data數(shù)據(jù)時(shí)帜矾,輸入會(huì)顯示異常翼虫,輸入內(nèi)容反向,輸入中文時(shí)會(huì)出現(xiàn)拼音(輸入任意內(nèi)容直接顯示)屡萤,綁定不同的data數(shù)據(jù)即可解決

<template>
  <div>
  <el-form :model="ruleForm" :rules="rules" status-icon ref="ruleForm">
    <table class="subTable">
      <col width="20%" />
      <col width="80%" />
      <tbody>
        <tr>
          <td><span class="red">*</span> 標(biāo)題</td>
          <td>
            <el-form-item prop="noticeTitle" :inline-message="true">
                <el-col :xs="{span:14,offset:5}" :sm="{span:8,offset:8}" :md="{span:8,offset:8}" :lg="{span:8,offset:8}" :xl="{span:4,offset:10}">
                    <el-input v-model.trim="ruleForm.noticeTitle" size="small"></el-input>
                </el-col>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <td><span class="red">*</span> 公告內(nèi)容</td>
          <td>
          <el-form-item prop="noticeContext">
            <el-col v-loading="quillUpdateImg">
                <quill-editor
                  v-model.trim="ruleForm.noticeContext"
                  ref="myQuillEditor"
                  :options="editorOption"
                >
                </quill-editor>
            </el-col>
           </el-form-item>
          </td>
        </tr>
        <tr>
          <td>默認(rèn)內(nèi)容</td>
          <td>
          <el-form-item prop="">
            <el-col v-loading="quillUpdateImg">
                <quill-editor
                  v-model.trim="defaultContent"
                  ref="myQuillEditor"
                  :options="editorOption"
                >
                </quill-editor>
            </el-col>
           </el-form-item>
          </td>
        </tr>
      </tbody>
    </table>
    <el-form-item>
      <el-col class="sfooter">
         <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
         <el-button @click="$router.go(-1);">取消</el-button>
      </el-col>
    </el-form-item>
  </el-form>
    <el-upload
            class="avatar-uploader"
            action=""
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :before-upload="beforeUpload">
    </el-upload>
  </div>
</template>
<script>
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    import { quillEditor } from 'vue-quill-editor'
  export default {
    components: {quillEditor},
    data() {
      var container =[
          ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
          ['blockquote', 'code-block'],

          [{'header': 1}, {'header': 2}],               // custom button values
          [{'list': 'ordered'}, {'list': 'bullet'}],
          [{'script': 'sub'}, {'script': 'super'}],      // superscript/subscript
          [{'direction': 'rtl'}],                         // text direction

          [{'size': ['small', false, 'large', 'huge']}],  // custom dropdown
          [{'header': [1, 2, 3, 4, 5, 6, false]}],

          [{'color': []}, {'background': []}],          // dropdown with defaults from theme
          [{'font': []}],
          [{'align': []}],
          ['link', 'image'],
          ['clean']                                         // remove formatting button
      ]
      return {
        ruleForm:{
            noticeTitle:'',
            noticeContext:'',
            htmlLink:''
        },
        rules:{
            noticeTitle:[
                { required: true,message: '請(qǐng)?zhí)顚憳?biāo)題', trigger: 'blur' }
            ],
            noticeContext:[
                { required: true,message: '請(qǐng)?zhí)顚懝鎯?nèi)容', trigger: 'blur' }
            ]
        },
       quillUpdateImg:false,
       defaultContent: 'XXX',
        // 富文本框參數(shù)設(shè)置
        editorOption: {  
          modules: {
            toolbar: {
              container: container,
              handlers: {
                'image': function (value) {
                    console.log(value)
                  if (value) {
                        document.querySelector('.avatar-uploader input').click()
                  } else {
                        this.quill.format('image', false);
                  }
                }
              }
            }
          }
        }
      }
    },
    created(){
        
    },
    methods:{
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                console.log(this.ruleForm);
              } else {
                console.log('error submit!!');
                return false;
              }
            });
         },
        beforeUpload() {
            // 顯示loading動(dòng)畫
            this.quillUpdateImg = true
        },
        
        uploadSuccess(res, file) {
  
            // 獲取富文本組件實(shí)例
            let quill = this.$refs.myQuillEditor.quill
            // 如果上傳成功
            if (res.state === 'SUCCESS' && res.url !== null) {
                // 獲取光標(biāo)所在位置
                let length = quill.getSelection().index;
                // 插入圖片  res.info為服務(wù)器返回的圖片地址
                quill.insertEmbed(length, 'image', res.url)
                // 調(diào)整光標(biāo)到最后
                quill.setSelection(length + 1)
            } else {
                this.$message.error('圖片插入失敗')
            }
            // loading動(dòng)畫消失
            this.quillUpdateImg = false
        },
   
        uploadError() {
            // loading動(dòng)畫消失
            this.quillUpdateImg = false
            this.$message.error('圖片插入失敗')
        }
    }
  }
</script>

<style scoped lang="scss">
 

.limit {
  height: 30px;
  border: 1px solid #ccc;
  line-height: 30px;
  text-align: right;

  span {
    color: #ee2a7b;
  }
}

.ql-snow .ql-editor img {
  max-width: 480px;
}

.ql-editor .ql-video {
  max-width: 480px;
}
.el-form{
    padding: 15px 20px;
  }
  .line {
    text-align: center;
  }
  table.subTable {
    width: 100%;
    border-collapse:collapse;
    font-size: 14px;
    color:#909399;
    text-align:center;
  }
  table.subTable th, table.subTable td {
    border: 1px solid #ebeef5;
    padding: 20px ;
  }
  .tableTile {
    border-top: 1px solid #ebeef5;
    border-left: 1px solid #ebeef5;
    border-right: 1px solid #ebeef5;
    padding:15px;
    color:#000;
    text-align: center;
  }

  .el-form-item {
    margin-bottom: 0;
  }
  .red {
      color:#f56c6c;
    }
   .sfooter {
    text-align: center;
    margin-top: 20px;
   }
   .message {
    margin-bottom: 10px;
   }

</style>
<style>
  .ql-toolbar.ql-snow {
    text-align:left;
  }  
 .ql-editor {
    min-height: 185px;

  }
  table.subTable .el-form-item__content {
    line-height:20px;
   }
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末珍剑,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子死陆,更是在濱河造成了極大的恐慌招拙,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件措译,死亡現(xiàn)場(chǎng)離奇詭異别凤,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)领虹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門规哪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人塌衰,你說我怎么就攤上這事诉稍◎鸺危” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵杯巨,是天一觀的道長(zhǎng)蚤告。 經(jīng)常有香客問我,道長(zhǎng)服爷,這世上最難降的妖魔是什么杜恰? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮仍源,結(jié)果婚禮上箫章,老公的妹妹穿的比我還像新娘。我一直安慰自己镜会,他們只是感情好檬寂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著戳表,像睡著了一般桶至。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上匾旭,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天镣屹,我揣著相機(jī)與錄音,去河邊找鬼价涝。 笑死女蜈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的色瘩。 我是一名探鬼主播伪窖,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼居兆!你這毒婦竟也來了覆山?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤泥栖,失蹤者是張志新(化名)和其女友劉穎簇宽,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吧享,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡魏割,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钢颂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钞它。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出须揣,到底是詐尸還是另有隱情盐股,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布耻卡,位于F島的核電站疯汁,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏卵酪。R本人自食惡果不足惜幌蚊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望溃卡。 院中可真熱鬧溢豆,春花似錦、人聲如沸瘸羡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)犹赖。三九已至队他,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間峻村,已是汗流浹背麸折。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留粘昨,地道東北人垢啼。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像张肾,于是被迫代替她去往敵國(guó)和親芭析。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355