如何使用vue js附加元素

Ant*_*oci 4 javascript vue.js vuejs2

我有可变的对象数组,其中一个包含字符串中的元素p。我想附加到元素div.profile-description

但结果是 vue js 返回原始 html 到div.profile-description

我想将字符串中的 p变成html 中的元素

var interviewees = [
  {name: 'Anthony', profession: 'Developer', description: '<p>Now he works in one</p><p>very famous company</p>'},
  {name: 'Erick', profession: 'Gamer', description: '<p>He is focusing on playing</p><p>the heroes of the storm game</p>'}
];

var setDescription = new Vue({
	el: '.profile-description',
	data: {
		interviewee_description: interviewees[1].description
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.js"></script>
<div class="profile-description">
  {{ interviewee_description }}
</div>
Run Code Online (Sandbox Code Playgroud)

Ser*_*ues 6

您需要使用v-html指令:

<div v-html="interviewee_description" class="profile-description"></div>
Run Code Online (Sandbox Code Playgroud)

警报:

在您的网站上动态渲染任意 HTML 可能非常危险,因为它很容易导致 XSS 漏洞。仅对受信任的内容使用 HTML 插值,切勿对用户提供的内容使用。 https://vuejs.org/v2/guide/syntax.html#Raw-HTML