鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 在element-ui的dialog中加入富文本框wangeditor,多次點擊

在element-ui的dialog中加入富文本框wangeditor,多次點擊后出現(xiàn)多個文本框疊加問題,如何解決?

如題,在寫一個后臺文章管理頁面,通過彈出dialog來編輯文章內(nèi)容,正文使用wangEditor來編輯,在編輯按鈕的響應(yīng)方法中加入的wangEditor的創(chuàng)建和配置代碼,結(jié)果如圖:
第一次點擊按鈕后,富文本框不正常顯示;
圖片描述
第二次點擊,顯示一個富文本框;
圖片描述
第三次點擊,顯示兩個個嵌套的富文本框;
圖片描述
第四次,顯示了三個……以此類推。
我的代碼是這么寫的:
彈出對話框按鈕:

<el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="success" @click="editArticle(scope.row)">編輯</el-button>
            <el-button size="small" type="info" @click="delArticle(scope.row)">刪除</el-button>
          </template>
</el-table-column>

對話框中對富文本框的定義:

<el-dialog title="修改精選文章" :visible.sync="dialogVisible">
        <el-form :model="articleInfo" label-width="160px" ref="articleInfo" :rules="articleRules">
            <el-form-item label="文章正文:" prop="Content">
                <div  id="editor" style="text-align:left"></div>
            </el-form-item>
        </el-form>
</el-dialog>

JS代碼中對對話框按鈕的響應(yīng)方法:

 editArticle(row){
          this.dialogVisible = true;
          this.articleInfo = row;
          console.log('row:', this.articleInfo);

          //富文本編輯器的代碼
          let self = this;
          var editor = new E('#editor');
          editor.customConfig.onchange = (html) => {
            self.articleInfo.Content = html;
          };
          editor.customConfig.uploadImgServer = '/api/v1/put_file';
          editor.customConfig.showLinkImg = false;
          editor.customConfig.customUploadImg = function (files, insert) {
            // files 是 input 中選中的文件列表
            // insert 是獲取圖片 url 后,插入到編輯器的方法
            //打印出來的是編輯器獲取到的圖片文件們,是一個圖片文件數(shù)組
            // console.log(files);
            //循環(huán)依次存儲圖片到服務(wù)器
            for(let i=0;i<files.length;i++){

              //創(chuàng)建form對象,將文件傳到后端接口
              let param = new FormData(); //創(chuàng)建form對象
              param.append('file',files[i],files[i].name);//通過append向form對象添加數(shù)據(jù)
              //param.append('chunk','0');//添加form表單中其他數(shù)據(jù)
              //  console.log(param.get('file')); //FormData私有類對象,訪問不到,可以通過get判斷值是否傳進去
              let config = {
                headers: {'Content-Type': 'multipart/form-data'}
              };

              myAxios.post('/api/v1/put_file',param,config)
                .then(res => {
                  console.log(res);

                  let url="http://"+res.data.data.url;
                  // 上傳代碼返回結(jié)果之后,將圖片插入到編輯器中
                  insert(url);
                })
                .catch(err => {
                  console.log(err)
                });
            }
          };

          editor.create();
        },

我自己嘗試過在關(guān)閉對話框時添加監(jiān)聽方法,然后關(guān)閉editor,但是無論是editor.close()還是editor.destory()都會提示沒有這個方法,求助。

回答
編輯回答
敢試

很明顯你每點一次editArticle方法就new E('#editor')一次,出現(xiàn)多個也不意外,你應(yīng)該先調(diào)用一次清楚,再new吧,
你創(chuàng)建的時候這個editor變量有存下來嗎?我看好像沒有this.editor什么的。
如果沒存你在editor = new E('#editor')后面先調(diào)用一次,editor.destory(),看看有沒有效果吧

2018年2月20日 02:51