我有可变的对象数组,其中一个是在字符串中包含元素 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/