Vue2Editor是一个简单易用且功能强大的Vue版本的富文本编辑器,其基于Quill.js和Vuejs构建!
简单易用、功能强大的富文本编辑器——Vue2Editor
第一种方式就是使用cdn或者
- npm install vue2-editor
- #或者使用
- yarn add vue2-editor
有两种方法可以设置和使用Vue2Editor。可以将其全局设置为Vue插件,也可以导入VueEditor组件以在本地注册并使用它。两种方法的例子如下
- import Vue from "vue";
- import Vue2Editor from "vue2-editor";
-
- Vue.use(Vue2Editor);
- // 基本用途-涵盖大多数情况
- import { VueEditor } from "vue2-editor";
-
- // 高级使用-HookQuill的API定制功能
- import { VueEditor, Quill } from "vue2-editor";
- <vue-editor v-model="content" />
-
- <script>
- import { VueEditor } from "vue2-editor";
-
- export default {
- components: { VueEditor },
-
- data: () => ({
- content: "
Some initial content
" - })
- };
- script>
如果选择使用自定义图像处理程序,则在选择照片时会发出一个事件。可以看到下面传递了3个参数。
- <div id="app">
- <vue-editor id="editor" useCustomImageHandler @imageAdded="handleImageAdded" v-model="htmlForEditor"> vue-editor>
- div>
-
- <script>
- import { VueEditor } from "vue2-editor";
- import axios from "axios";
- export default {
- components: {
- VueEditor
- },
-
- data() {
- return {
- htmlForEditor: ""
- };
- },
-
- methods: {
- handleImageAdded: function(file, Editor, cursorLocation, resetUploader) {
- // An example of using FormData
- // NOTE: Your key could be different such as:
- // formData.append('file', file)
-
- var formData = new FormData();
- formData.append("image", file);
-
- axios({
- url: "https://fakeapi.yoursite.com/images",
- method: "POST",
- data: formData
- })
- .then(result => {
- let url = result.data.url; // Get url from response
- Editor.insertEmbed(cursorLocation, "image", url);
- resetUploader();
- })
- .catch(err => {
- console.log(err);
- });
- }
- }
- };
- script>
- <div>
- <button @click="setEditorContent">Set Editor Contentbutton>
- <vue-editor v-model="content" />
- div>
-
- <script>
- import { VueEditor } from "vue2-editor";
-
- export default {
- components: { VueEditor },
-
- data: () => ({
- content: null
- }),
-
- methods: {
- setEditorContent() {
- this.content = "
Html For Editor
"; - }
- }
- };
- script>
- <div id="app">
- <vue-editor id="editor1" v-model="editor1Content">vue-editor>
- <vue-editor id="editor2" v-model="editor2Content">vue-editor>
- div>
-
- <script>
- import { VueEditor } from "vue2-editor";
-
- export default {
- components: {
- VueEditor
- },
-
- data() {
- return {
- editor1Content: "
Editor 1 Starting Content
", - editor2Content: "
Editor 2 Starting Content
" - };
- }
- };
- script>
-
- <style>
- #editor1,
- #editor2 {
- height: 350px;
- }
- style>
- <vue-editor v-model="content" :editor-toolbar="customToolbar" />
-
- <script>
- import { VueEditor } from "vue2-editor";
-
- export default {
- components: { VueEditor },
-
- data: () => ({
- content: "
Html For Editor
", - customToolbar: [
- ["bold", "italic", "underline"],
- [{ list: "ordered" }, { list: "bullet" }],
- ["image", "code-block"]
- ]
- })
- };
- script>
- <vue-editor v-model="content" :editor-toolbar="customToolbar" />
-
- <script>
- import { VueEditor } from "vue2-editor";
-
- export default {
- components: { VueEditor },
-
- data: () => ({
- content: "
Html For Editor
", - customToolbar: [
- ["bold", "italic", "underline"],
- [{ list: "ordered" }, { list: "bullet" }],
- ["image", "code-block"]
- ]
- })
- };
- script>
- <div>
- <vue-editor v-model="content" />
- <div>{{ content }}div>
- div>
-
- <script>
- import { VueEditor } from "vue2-editor";
-
- export default {
- components: { VueEditor },
-
- data: () => ({
- content: "
Some initial content
" - })
- };
- script>
Vue2Editor是一个简单易用的富文本编辑器,如果没有复杂的需求,你可以毫无保留的使用它,如果你需要复杂的功能,也可以使用其自定义能力进行自定义扩展!
链接:https://www.jianshu.com/p/424556c68cd6