可以用v-on
指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码。
DOM是文档对象模型, HTML DOM允许JavaScript对HTML事件作出反应
v-on: 简写 @
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<span>{{message}}</span>
<button v-on:click="login">登录</button>
<a href="javascript:;" @click="register">注册</a>
<button @click="add(counter)">点击+1</button>
</div>
</body>
<script>
var vm = new Vue({
el: "#app",
data: {
message: "hello",
counter: 1,
total: 0,
},
methods: {
login: function () {
alert("我被点击了");
},
register: function () {
alert("注册");
},
add: function (counter) {
this.total += counter;
alert(this.total);
}
}
})
</script>
</html>