vue教程——12 ref获取DOM元素和组件与watch-监听路由地址的改变
一看就会系列,在这不多做解释
一 ref获取DOM元素和组件
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title></title>
<!-- 引入vue.js -->
<script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script>
</head>
<body>
<div id='app'>
<input type="button" value="获取元素" @click="getElement">
<h3 ref="myh3">哈哈哈,好逗啊</h3>
<hr>
<login ref="mylogin"></login>
</div>
</body>
<script>
let login = {
template:"<h1>login组件</h1>",
data(){
return {
msg:"子组件msg"
}
},
methods:{
}
}
// 实例化vue对象
let vm = new Vue({
// 绑定对象
el:'#app',
data:{
msg:"父组件msg"
},
methods:{
getElement(){
// console.log(document.getElementById('myh3').innerText)
console.log(this.$refs.myh3.innerText)
console.log(this.$refs.mylogin.msg)
}
},
components:{
login
}
})
</script>
</html>
二 watch-监听路由地址的改变
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title></title>
<!-- 引入vue.js -->
<script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script>
<!-- 引入路由模块 -->
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id='app'>
<router-link to="/login">登录</router-link>
<router-link to="/register">注册</router-link>
<router-view></router-view>
</div>
</body>
<script>
// 创建组件模板对象
let login = {
template:"<h1>登录组件</h1>",
}
let register = {
template:"<h1>注册组件</h1>",
}
let router = new VueRouter({
routes:[
{path:'/',redirect:'login'},
{path:'/login',component:login},
{path:'/register',component:register}
]
})
// 实例化vue对象
let vm = new Vue({
// 绑定对象
el:'#app',
data:{
},
methods:{
},
router,
watch:{
"$route.path":function(newVal,oldVal){
console.log("new:"+newVal+"--old:"+oldVal)
if(newVal == "/login"){
console.log("欢迎进入登录页面")
}else if(newVal == "/register"){
console.log("欢迎进入注册页面")
}
}
}
})
</script>
</html>