先看下面的这一串很简单代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<input type="text" ref ="input" value="真是尴尬">
<button @click="headleClick" ref = "button">
点击
</button>
</div>
<script src="./vue.js"></script>
<script>
new Vue({
el:"#app",
data:{
},
methods:{
headleClick() {
console.log(this.$refs.input.value)
console.log(this.$refs.button)
}
}
})
</script>
</body>
</html>
通过上面的代码,便可知vue如果想操作DOM的时候需要给该元素添加一个属性 ref 然后通过 $refs的方式来获取该DOM元素