vue使用v-html或者使用innerHTML加入的html字符串函数不生效

每日鸡汤:每个你想要学习的瞬间都是未来的你向自己求救

vue使用v-html, 或者使用innerHTML加入的dom怎么设置函数调用?比如


const div = document.createElement('div')
div.innerHTML = `<span onclick="send"e> click me</span>`

如果你在vue中直接这样写肯定不行,点击这个div的时候,他会提示找不到send方法,所以解决办法是咋mounted之后给window挂载一个send方法就行了

// vue 的mounted 方法
mounted() {
    // 给window挂载一个已有的send方法,这样上面的html代码就可以找到send方法了
    window.send = this.send
}

使用v-html同理,因为v-html指令的本质就是使用了innerHTML方法

猜你喜欢

转载自blog.csdn.net/qq_17335549/article/details/131252928