0

I´m currently learning vue, and came accross issue when I rander an HTML text. This is my component template, its an WYSIWYG.

<template>
 <div id='editor_container'>
  <slot name="header" />
  <div id='editor' ref='editorPost'></div>
  <slot name="footer" />
 </div>
</template>

import { tempDOC } from './helpers/docFormats'

I created simple function to send data for testing.

      templateEdit () {
        const editor = this.$refs.editorPost.innerHTML = tempDOC
      }

On my tempDOC.js file I export string:

export const tempDOC = Please enter your name to continue

When I render innerHTML content from tempDOC into the $refs.editorPost(editor WYSIWYG), the value is getting duplicated.

Editor result:

Please enter your name to continue


Please enter your name to continue


Bellow is the inspect HTML.

<div id="editor" spellcheck="false" contenteditable="true">
<p>Please enter   your name to continue</p>

Please enter your name to continue
</div>

Not sure whey the values are getting duplicated,I debug app using chrome and I see this is being called after my function. I don´t have that piece code on my side.

    this._observer = new MutationObserver(function (mutations) {
  _this._handleMutations(mutations);
});

}

2
  • how are you consuming this component? I see two slot's but i'm not sure how you're using them. can you reproduce this issue in jsfiddle or codepen? Commented Aug 7, 2018 at 23:31
  • How are you calling templateEdit? Commented Aug 8, 2018 at 5:01

1 Answer 1

1

It is a bad practice to use refs for this purpose. You should use the Data function of Vue to make a variable and change it / reach it's value through the vue this.

<template>
 <div id='editor_container'>
  <slot name="header" />
  <div id='editor'>{{editorPost}}</div>
  <slot name="footer" />
 </div>
</template>

<script>
    export default {
        data() {
            return {
                editorPost: "this is the inner HTML",
            }
        },
    }
</script>

Now change the editorPost to change the innerHTML.

Sign up to request clarification or add additional context in comments.

1 Comment

can you please help, how to get innerHTML of some div by id in editorPost ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.