【发布时间】:2019-03-26 22:24:08
【问题描述】:
我使用 VueJs,并用它创建了以下组件。
var ComponentTest = {
props: ['list', 'symbole'],
data: function(){
return {
regexSymbole: new RegExp(this.symbole),
}
},
template: `
<div>
<ul>
<li v-for="item in list"
v-html="replaceSymbole(item.name)">
</li>
</ul>
</div>
`,
methods: {
replaceSymbole: function(name){
return name.replace(this.regexSymbole, '<span v-on:click="test">---</span>');
},
test: function(event){
console.log('Test ...');
console.log(this.$el);
},
}
};
var app = new Vue({
el: '#app',
components: {
'component-test': ComponentTest,
},
data: {
list: [{"id":1,"name":"@ name1"},{"id":2,"name":"@ name2"},{"id":3,"name":"@ name3"}],
symbole: '@'
},
});
这是我的html代码
<div id="app">
<component-test :list="list" :symbole="symbole"></component-test>
</div>
当我点击“li”标签内的“span”标签时,没有任何附加内容。
我没有任何警告和任何错误。
当我点击“span”标签时如何调用我的组件方法“test”。
本案例如何实现点击事件。
【问题讨论】:
-
如果您检查生成的 html,我希望您在 span 元素上看到文字 v-on:click 属性,对吧?
-
是的,我看到 v-on:click in all span 标签
-
Vue 不会解释您动态创建的 span 标签。您可以使用纯 JavaScript,也可以以编程方式将 Vue 组件渲染到 span 标签中,这会触发点击事件。
-
“你不能使用 v-html 来组合模板部分,因为 Vue 不是基于字符串的模板引擎。相反,组件是 UI 重用和组合的基本单元。” (source)。您也可以查看this answer
标签: vue.js