javascript - 如何使用vue js追加元素

标签 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
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.js"></script>
<div class="profile-description">
  {{ interviewee_description }}
</div>

最佳答案

你需要使用 v-html 指令:

<div v-html="interviewee_description" class="profile-description"></div>

提醒:

Dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to XSS vulnerabilities. Only use HTML interpolation on trusted content and never on user-provided content. https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML

关于javascript - 如何使用vue js追加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43671375/

相关文章:

javascript - 使用自定义指令在表单必填字段上动态设置 ngIf

vue.js - 如何使用 VeeValidate 在 onblur 事件时停止验证

vue.js - 如何使用 vue-resource 在表单提交上添加预加载器和成功消息

javascript - this.$set 更新数组不起作用(VueJS)

javascript - 在子 vue 组件数据函数中访问 props?

javascript - 使用我网站上的按钮更改 h1/h2/h3 的字体大小(使用本地存储)

javascript - 合并数组中具有两个相同键值对的对象

javascript - 如何在Vue项目中使用Chance js?

javascript - vue js 中的自定义标签 - <myTag_vue> ... </> 之间不能插入任何内容?

vue.js - 在 v-if 中使用方法函数