注意,在父组件中可以使用this.$refs.属性名  获取任何元素的属性和方法,子组件不可以获取父组件中的

<!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="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div class="app">
<input type="button" @click="show" value="点击">
<!-- 设置元素ref 属性 -->
<h1 ref="chuandi">中国是伟大的祖国</h1>
<hr>
<log ref="mylog"></log>
</div> <template id="log">
<div>
<input type="button" value="获取元素" @click="comfunc">
<h1>你说的很对啊</h1>
</div>
</template> <script>
var vm=new Vue({
el:'.app',
data:{},
methods: {
show(){
// 获取ref属性为chuandi的内部文本
console.log(this.$refs.chuandi.innerText); //获取到了h1 元素的文本
console.log(this.$refs.mylog.name); //获取到了子组件的data属性
console.log(this.$refs.mylog.add); //获取到了子组件的方法 }
}, components:{
log:{
template:'#log',
data(){
return{
name:'duwei'
}
},
methods: {
add(){ console.log('调用了子组件的方法'); },
comfunc(){
console.log(this.$refs.chuandi.innerText); //报错 innerText没有定义, 子组件不能通过ref 来获取父组件的属性方法
// 需要使用props }
},
} }
})
</script>
</body>
</html>

最新文章

  1. 530 User cannot log in, home directory inaccessible.
  2. 在计算机 . 上没有找到服务 WAS
  3. selenium获取html的表格单元格数据
  4. 获取php的配置
  5. hadoop2 作业执行过程之yarn调度执行
  6. 测试使用Windows Live Writer
  7. 后台地址报错:Service Unavailable
  8. 子查询有OR无法展开,改写成union
  9. #ifndef #define #endif 防止头文件被重复引用
  10. aspose.words 处理word转PDF
  11. HDU 2087 剪花布条 KMP
  12. 201521123098 《Java程序设计》第14周学习总结
  13. 大话python模块与包
  14. Django中不返回QuerySets的API -- Django从入门到精通系列教程
  15. Kafka--消息队列
  16. 3173. 【GDOI2103模拟3.17】扫雷游戏(搜索 + 剪枝)
  17. 让wampserver2.5.exe支持sql server数据库的方法
  18. iptables精通
  19. grid的简单使用
  20. VB6 red write DB using Microsoft DAO 3.6 Object Library

热门文章

  1. 深入学习c++--智能指针(四)--使用建议
  2. Windows下MariaDB数据库安装图文教程
  3. NDK开发和NDK双进程守护
  4. 【k8s node断电重启】
  5. css文本超出部分省略号&amp;CSS强制换行总结
  6. centos下通过yum安装redis-cli
  7. 2019年春季学期《C语言程序设计II》课程总结
  8. mac install azure-cli
  9. [转帖]iis最大并发连接数、队列长度、最大并发线程数、最大工作进程数
  10. Spring之2:HierarchicalBeanFactory接口