vue3中使用vue-quill-editor报错
时间: 2023-08-27 15:07:06 浏览: 178
在 Vue 3 中使用 Vue-Quill-Editor 可能会出现以下错误:
```
[vue-composition-api] "Uncaught TypeError: Cannot read property '_vm' of undefined"
```
这是因为 Vue 3 中使用了 Composition API,而 Vue-Quill-Editor 还没有完全适配 Composition API。解决该问题可通过在 `setup()` 函数中引入 `onMounted`、`onUnmounted` 等钩子函数,然后手动初始化 Quill 编辑器。
以下是一个示例:
```vue
<template>
<div>
<div ref="editor"></div>
</div>
</template>
<script>
import { onMounted, onUnmounted, ref } from 'vue';
import Quill from 'quill';
import 'quill/dist/quill.snow.css';
export default {
setup() {
const editor = ref(null);
onMounted(() => {
const quill = new Quill(editor.value, {
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'],
[{ list: 'ordered' }, { list: 'bullet' }],
['link', 'image'],
],
},
theme: 'snow',
});
quill.on('text-change', () => {
console.log(quill.root.innerHTML);
});
// save the instance of Quill editor
editor.value.__quill = quill;
});
onUnmounted(() => {
// destroy the instance of Quill editor
editor.value.__quill.destroy();
editor.value.__quill = null;
});
return {
editor,
};
},
};
</script>
```
在上述示例中,我们在 `setup()` 函数中使用 `onMounted` 和 `onUnmounted` 钩子函数来初始化和销毁 Quill 编辑器。同时,我们在 `onMounted` 中监听 `text-change` 事件,以便在编辑器内容发生变化时执行一些操作。最后,我们将 Quill 实例保存在 `editor.value.__quill` 中,以便在其他地方访问该实例。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)