ref 獲取DOM元素和組件



<!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>
<script src="./js/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<input type="button" value="獲取元素" @click="getElement" ref="mybtn">
<h3 id="myh3" ref="myh3">哈哈哈,今天天氣太好了。。。</h3>
<hr>
<login ref="mylogin"></login>
</div>
<script>
var login={
template:'<h1>登錄組件</h1>',
data(){
return{
msg:'son msg'
}
},
methods:{
show(){
console.log('調用了子組件的方法')
}
}
}
// 創建Vue實例,得到ViewModel
var vm=new Vue({
el:'#app',
data:{},
methods:{
getElement(){
//console.log (document.getElementById('myh3).innerText)
//ref 是英文單詞【reference】 值類型和引用類型 referenceError
//console.log(this.$refs.mylogin.msg)
//this.$refs.mylogin.show()
}
},
components:{
login
}
})
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM