The difference between the three methods of text rendering in Vue {{}}, v-html, and v-text

v-text and { {}}

Description: Display normal text

Shorthand: { {}}

grammar:

1:<p v-text="msg"></p>
2:<p>{
   
   {msg}}</p>

Instance

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <div id="app">
       	   <p v-text='msg1'></p>
       	   <p>{
   
   {msg2}}</p>
        </div>
    </body>
    <script src="../vue.js"></script>
    <script>
       var vm = new Vue({
            el: '#app',
            data: {
              msg1:'这是一个消息',
              msg2:'这是另外一个消息'
            }
        })
    </script>
</html>

Page effect:

v-html

Description: Output real HTML

Syntax: <p v-html='msg'></p>

Examples:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <div id="app">
       	   <p v-html='msg3'></p>
        </div>
    </body>
    <script src="../vue.js"></script>
    <script>
       var vm = new Vue({
            el: '#app',
            data: {
              msg1:'这是一个消息',
              msg2:'这是另外一个消息',
              msg3:'<strong>这是一个html<strong>'
            }
        })
    </script>
</html>

Page effect:

the difference

v-html outputs real HTML, v-text only renders tags and cannot parse HTML code

Examples:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <div id="app">
       	   <p v-text='msg3'></p>
       	   <p>{
   
   {msg3}}</p>
       	   <p v-html='msg3'></p>
        </div>
    </body>
    <script src="../vue.js"></script>
    <script>
       var vm = new Vue({
            el: '#app',
            data: {
              msg1:'这是一个消息',
              msg2:'这是另外一个消息',
              msg3:'<strong>这是一个html<strong>'
            }
        })
    </script>
</html>

Page effect:

to sum up

v-text and { {}} expressions render data without analysing tags. If there are tags, they will be directly output to the page as characters.

  v-html can not only render data, but also parse tags.

Guess you like

Origin blog.csdn.net/dkm123456/article/details/114642489