[英]Render Vue.js @click directive outside of html tag
I have a link set up like so: 我有这样设置的链接:
<a href="#" @click="modal=true">Open modal</a>
And here's the data: 这是数据:
export default {
data () {
return {
modal: false
}
}
}
Is there a workaround to trigger the click event, without actually having to place the @click directly in the tag? 是否有一种解决方法来触发click事件,而无需实际将@click直接放置在代码中? The expected result would be the same however the @click would be defined elsewhere in the vue instance.
预期结果将是相同的,但是@click将在vue实例中的其他位置定义。
You could try this: 您可以尝试以下方法:
export default {
data () {
return {
modal: false
}
},
mounted () {
var self = this;
this.$el.querySelector('a').addEventListener('click', function () {
Vue.set(self, 'modal', true);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.