javascript ReferenceError:尝试在svelte应用程序中使用媒体编辑器时未定义文档

odopli94  于 6个月前  发布在  Java
关注(0)|答案(1)|浏览(69)

我尝试在我的Svelte应用程序中使用medium-editor,如下所示。

<script lang="ts">
  import { onMount } from 'svelte';
  import 'medium-editor/dist/css/medium-editor.min.css';
  import 'medium-editor/dist/css/themes/default.min.css';
  import MediumEditor from 'medium-editor';

  let editor;

  onMount(() => {
    editor = new MediumEditor('#editable', {
      // medium-editor options can be added here
    });
  });

</script>

<div class="app">
    <div id="editable" contenteditable="true">
        <!-- Your initial content goes here -->
      </div>
</div>

<style>
    
</style>

字符串
然而,当运行应用程序时,我得到以下错误,尽管onMount函数已经使用。
参考错误:在Object中未定义文档。(/Users/pavindu/Projects/chronicle/app/node_modules/medium-editor/dist/js/medium-editor.js:6:22)at Module._compile(node:internal/modules/cjs/loader:1256:14)at Module._extensions..js(node:internal/modules/cjs/loader:1310:10)在Module.load(node:internal/modules/cjs/loader:1119:32)at Module._load(node:internal/modules/cjs/loader:960:12)。(节点:internal/modules/esm/translators:169:29),ModuleJob.run网址:www.example.com(节点:internal/modules/esm/module_job:194:25)

cngwdvgl

cngwdvgl1#

您可以使用代码来确定您是否在浏览器中

import { browser } from '$app/environment';

onMount(() => {
  if (browser) {
    editor = new MediumEditor('#editable', {
      // medium-editor options can be added here
    });
  }

字符串
或者更好的方法是按照sveltekit文档,您可以在onMount方法中导入

import { onMount } from 'svelte';

onMount(async () => {
    let editor = await import('medium-editor');
    editor = new MediumEditor('#editable', {
      // medium-editor options can be added here
    });
});


查看SvelteKit常见问题解答如何使用依赖于文档或窗口的客户端专用库?

相关问题