
如上图所示,没那么多废话,直接上代码
html中
<div id="app">
<div :html="tempHtml"></div>
</div>
vue中
new Vue({
el: '#app',
data() {
return {
tempHtml: ''
}
},
created() {
this.getHtml()
},
mounted() {
window.clickFun = this.clickFun;
},
methods: {
getHtml() {
let template = "<el-button type='primary' size='mini' @click='clickFun()'>查询</el-button>"
this.tempHtml = template;
},
clickFun() {
console.log(121212);
}
},
render(h) {
const com = Vue.extend({
template: eval('`' + this.tempHtml + '`')
});
return h(com, {});
}
})
这篇文章展示了在Vue.js中如何动态生成HTML内容并绑定点击事件。代码创建了一个Vue实例,将`tempHtml`数据绑定到`div`元素内,并在`created`与`mounted`钩子函数中设置和挂载了`clickFun`方法。`getHtml`方法用于生成包含点击事件的按钮元素模板,而`render`函数则使用`Vue.extend`创建并渲染了这个动态模板。
3869

被折叠的 条评论
为什么被折叠?



