在nuxt中使用富文本編輯器quill


在nuxtjs中使用quill富文本編輯器的時候遇到了一些問題,在此記錄

1.下載

npm i quill --s
npm i vue-quill-editor --s

2.在plugins下面新建文件vue-quill-editor.js寫入以下內容,主要是用了自定義的工具欄

import Vue from 'vue';
import VueQuillEditor from 'vue-quill-editor';
import Quill from 'vue-quill-editor/node_modules/quill';
const fontSizeStyle = Quill.import('attributors/style/size');
fontSizeStyle.whitelist = ['12px', '14px', '16px', '18px', '20px', '24px', '36px']; // 自定義quill的工具欄
Quill.register(fontSizeStyle, true);
Vue.use(VueQuillEditor);

3.在nuxt.config.js中引入插件和quill的css,editor.css是我自定義部分的css樣式。

 

css: [
    'element-ui/lib/theme-chalk/index.css',
    '~assets/css/basic.css',
    '~assets/css/editor.css', // 編輯器css
    'quill/dist/quill.snow.css',
    'quill/dist/quill.bubble.css',
    'quill/dist/quill.core.css'
  ],
  /*
      ** Plugins to load before mounting the App
      */
  plugins: [
    '@/plugins/element-ui',
    '~/plugins/vue-echarts',
    '~/plugins/awe-dnd',
    { src: '@/plugins/vue-quill-editor', ssr: false },
    { src: '@/plugins/axios' },
    { src: '@/plugins/gmap' }
  ],

 4.自定義樣式:在assets/css/editor.css

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="12px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="12px"]::before {
    content: '12px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="14px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="14px"]::before {
    content: '14px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="16px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="16px"]::before {
    content: '16px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="18px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="18px"]::before {
    content: '18px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="20px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="20px"]::before {
    content: '20px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
    content: '14px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="24px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="24px"]::before {
    content: '24px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="36px"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="36px"]::before {
    content: '36px';
}

.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="12px"]::before{
    font-size: 12px;
}
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="14px"]::before{
    font-size: 14px;
}
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="16px"]::before{
    font-size: 16px;
}
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="18px"]::before{
    font-size: 18px;
}
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="20px"]::before{
    font-size: 20px;
}
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="24px"]::before{
    font-size: 24px;
}
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="36px"]::before{
    font-size: 36px;
}

5.頁面的引用

<client-only>
        <quill-editor
          ref="editor"
          v-model="newsInfo.content"
          :options="editorOption"
          @change="onEditorChange($event)"
        />
      </client-only>

options的配置,用的ts寫法

private editorOption: any = {
      modules: {
        toolbar: {
          container: ['bold', 'italic', 'underline', { indent: '-1' }, { align: [] }, { indent: '+1' },
            { header: 1 }, { header: 2 },
            { size: ['12px', '14px', '16px', '18px', '20px', '24px', '36px'] },
            // { size: ['small', false, 'large', 'huge'] },
            'link', 'image'], // 加粗 斜體 下划線 刪除線
          handlers: {
            image: () => {
              (this.$refs.quillImg as any).click();
            }
          }
        }
      }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM