v-text和v-html的区别

v-text和v-html的区别

  • v-text和{{}}一样
  • v-text不解释标签,v-html解释标签
  • 解释html标签可能导致xss攻击,不要使用用户提供的内容插值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<html>
<head>
<title></title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<p>{{msg}}</p>
<p v-text="msg"></p>
<p v-html="msg"></p>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app', //element
data: {
msg: '<strong>Hello</strong> Vue!',
}
})
</script>
</body>
</html>

效果:

{{}}:<strong>Hello</strong> Vue!
v-text:<strong>Hello</strong> Vue!
v-html:Hello Vue!