vue的自定义组件中可以定义事件吗
1个回答
展开全部
可以啊,只要在写成<h1 @click="hi()">{{a}}</h1>,然后在methods里写hi这个函数就行了,还是说你想添加两个点击事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="vue.js"></script>
<title>Title</title>
</head>
<body>
<div id="app">
<!-- <h1 is="test"></h1> -->
<test></test>
</div>
<script type="text/javascript">
var mytest = Vue.extend({
template: '<h1 @click="hi()">{{a}}</h1>',
data: function () {
return{
a: "this is a test"
}
},
methods: {
hi: function() {
alert(this.a);
}
}
});
var vm = new Vue({
el: "#app",
components: {
"test": mytest
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="vue.js"></script>
<title>Title</title>
</head>
<body>
<div id="app">
<!-- <h1 is="test"></h1> -->
<test></test>
</div>
<script type="text/javascript">
var mytest = Vue.extend({
template: '<h1 @click="hi()">{{a}}</h1>',
data: function () {
return{
a: "this is a test"
}
},
methods: {
hi: function() {
alert(this.a);
}
}
});
var vm = new Vue({
el: "#app",
components: {
"test": mytest
}
});
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询