Home  >  Q&A  >  body text

Using Vue2 v-html directive, is it possible to render rich text with `script`?

I want a piece of rich text to be rendered in vue, but this rich text contains a script statement. I tested it locally and did not execute the statement.
How to solve this problem?

Sample code:

<template>
    <p v-html="testHtml"></p>
</template>

<script>
  export default {
    data () {
        return {
            testHtml: '<p><script>console.log("test")</script><p><span>测试测试</span></p><p>'
        }
    }
  }
</script>

Thank you~

给我你的怀抱给我你的怀抱2672 days ago1046

reply all(3)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-26 10:58:14

    No, vue will not render the script tag in the page. I want to insert a third-party advertising script. You have several ways

    1. Rewrite a page, vue load this iframe, this performance will be poor, you can use the srcdoc attribute of iframe, it will be better

    2. If it must be inserted into the page, then after the list rendering is completed, pass document.createElement('script'), and then appendenter the page

    reply
    0
  • 怪我咯

    怪我咯2017-06-26 10:58:14

    Write a script command

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-26 10:58:14

    Post the code and look at the problem description. Isn’t it a good idea to write like this

    reply
    0
  • Cancelreply