vue v html replace,vuejs 2 v-for :key not working, html being replace

本文探讨了Vue.js框架中v-html指令的行为特点及其潜在问题。通过实例对比展示了当使用v-html更新DOM时,Vue并不会重新渲染整个列表,除非采用组件形式。文章还提供了将动态HTML内容封装为组件的解决方案,并附带了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I had a little play with this and it appears that Vue does not re-render the entire list when using or if you use a component but it does with v-html. Heres the fiddle for the comparison:

https://jsfiddle.net/cxataxcf/

The key actually isn't needed here because the list isn't being re-ordered, so your issue isn't to do with :key but rather with v-html. Heres what the docs say about v-html:

The contents are inserted as plain HTML - data bindings are ignored. Note that you cannot use v-html to compose template partials, because Vue is not a string-based templating engine. Instead, components are preferred as the fundamental unit for UI reuse and composition.

So I guess this is where the problem lies.

It might be worth raising an issue on Vue's github page to see whether this is the expected behavior for v-html, but Vue 2.0 is much more heavily focused on components than vue 1.x and doesn't appear to recommend using v-html, so it may just be that you need to re-factor your code to use components instead.

Edit

The solution to this problem is to simply wrap the code in a component and pass the HTML as a prop:

Vue.component('unknown-html', {

props: {

html: ""

},

template: '

})

The markup:

And the View model:

var app = new Vue({

el: '#app',

data: {

numInputs: 1,

stuff: [{

'html':''

}, {

'html':'Foo'

}]

}

})

Here's the JSFiddle: https://jsfiddle.net/wrox5acb/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值