SimpleMDE Markdown编辑器在使用Vue挂载时被禁用。

huangapple go评论63阅读模式
英文:

SimpleMDE markdown editor disabled when mounted with vue

问题

以下是您提供的HTML代码的中文翻译:

我正在尝试在我的HTML页面中使用Vue添加一个Markdown编辑器。然而,Markdown编辑器似乎出现了问题。

以下是页面的简单结构:

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- js -->
    <script src="https://unpkg.com/vue@3.0.11/dist/vue.global.js"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
</head>

<body>
<div id="vueapp">
    <div class="col-lg-12 col-md-12">
        <div class="form-group">
            <label>描述:</label>
            <textarea id="job-description"></textarea>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
<script type="module">
    var jobDescriptionEditor = new SimpleMDE({ element: document.getElementById("job-description") });
    const app = Vue.createApp({})
    const mountedApp = app.mount('#vueapp')
</script>
</body>
</html>

当移除Vue div "vueapp" 时,编辑器恢复正常功能。

以下是不包含Vue的HTML代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- js -->
    <script src="https://unpkg.com/vue@3.0.11/dist/vue.global.js"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
</head>

<body>
    <div class="col-lg-12 col-md-12">
        <div class="form-group">
            <label>描述:</label>
            <textarea id="job-description"></textarea>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
    <script type="module">
        var jobDescriptionEditor = new SimpleMDE({ element: document.getElementById("job-description") });
        const app = Vue.createApp({})
        const mountedApp = app.mount('#vueapp')
    </script>
</body>
</html>

是否有人知道如何解决此问题或者我可以嵌入的其他Markdown编辑器?我不能移除Vue,因为我需要响应性。

英文:

I am trying to add a markdown editor to my html page working with vue. However the markdown editor seems to be broken.

Here is a simple structure of the page

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;

    &lt;head&gt;
        &lt;!-- js --&gt;
        &lt;script src=&quot;https://unpkg.com/vue@3.0.11/dist/vue.global.js&quot;&gt;&lt;/script&gt;

        &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css&quot;&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div id=&quot;vueapp&quot;&gt;
        &lt;div class=&quot;col-lg-12 col-md-12&quot;&gt;
        &lt;div class=&quot;form-group&quot;&gt;
           &lt;label&gt;Description:&lt;/label&gt;
           &lt;textarea id=&quot;job-description&quot;&gt;&lt;/textarea&gt;
        &lt;/div&gt;
        &lt;/div&gt;
     &lt;/div&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;module&quot;&gt;
	    var jobDescriptionEditor = new SimpleMDE({ element: document.getElementById(&quot;job-description&quot;) });
      const app = Vue.createApp({})
      const mountedApp = app.mount(&#39;#vueapp&#39;)
    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;

<!-- end snippet -->

When the vue div vueapp is removed, the editor is back to being functional.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

        &lt;!DOCTYPE html&gt;
        &lt;html lang=&quot;en&quot;&gt;

        &lt;head&gt;
            &lt;!-- js --&gt;
            &lt;script src=&quot;https://unpkg.com/vue@3.0.11/dist/vue.global.js&quot;&gt;&lt;/script&gt;

            &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css&quot;&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;div class=&quot;col-lg-12 col-md-12&quot;&gt;
            &lt;div class=&quot;form-group&quot;&gt;
               &lt;label&gt;Description:&lt;/label&gt;
               &lt;textarea id=&quot;job-description&quot;&gt;&lt;/textarea&gt;
            &lt;/div&gt;
            &lt;/div&gt;
        &lt;script src=&quot;https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js&quot;&gt;&lt;/script&gt;
        &lt;script type=&quot;module&quot;&gt;
    	    var jobDescriptionEditor = new SimpleMDE({ element: document.getElementById(&quot;job-description&quot;) });
          const app = Vue.createApp({})
          const mountedApp = app.mount(&#39;#vueapp&#39;)
        &lt;/script&gt;
        &lt;/body&gt;
        &lt;/html&gt;

<!-- end snippet -->

Does Anybody know how to fix this or any other markdown editor that I can embed. I can't remove vue as I need the reactivity.

答案1

得分: 1

Vue会用它自己的节点替换#vueapp内部的节点。您必须使用Vue的节点而不是初始节点。因此,在mounted()钩子中初始化编辑器:

const app = Vue.createApp({
  mounted(){
    const element = this.$refs.jobDescriptionRef
    new SimpleMDE({element})
  }
})
app.mount('#vueapp')
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">

<div id="vueapp">
  <div class="col-lg-12 col-md-12">
    <div class="form-group">
      <label>Description:</label>
      <textarea ref="jobDescriptionRef"></textarea>
    </div>
  </div>
</div>
<script src="https://unpkg.com/vue@3.0.11/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>

当前方式下,您初始化编辑器,它会向其根元素添加元素。然后,所有这些都传递给Vue,Vue将其编译成模板并用自己的节点替换现有节点,包括编辑器元素。因此,在检查器中看起来相同,但所有事件都消失了。

英文:

Vue replaces the nodes inside #vueapp with its own nodes. You have to use the node from Vue instead of the initial one. So initialize the editor in the mounted() hook:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const app = Vue.createApp({
  mounted(){
    const element = this.$refs.jobDescriptionRef
    new SimpleMDE({element})
  }
})
app.mount(&#39;#vueapp&#39;)

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css&quot;&gt;

&lt;div id=&quot;vueapp&quot;&gt;
  &lt;div class=&quot;col-lg-12 col-md-12&quot;&gt;
    &lt;div class=&quot;form-group&quot;&gt;
      &lt;label&gt;Description:&lt;/label&gt;
      &lt;textarea ref=&quot;jobDescriptionRef&quot;&gt;&lt;/textarea&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3.0.11/dist/vue.global.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

The way it is, you initialize the editor, which adds elements to its root element. Then all of it is passed into Vue, which compiles it into a template and the replaces the existing nodes with its own, including the editor elements. So it looks the same in the inspector, but all events are gone.

huangapple
  • 本文由 发表于 2023年6月19日 13:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定