这篇文章主要介绍了vue 使用ref 让父组件调用子组件的方法,需要的朋友可以参考下
父级组件上的三个按钮可以
调用子组件loading的三个方法,执行不同的操作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="vue.js" charset="utf-8"></script>
</head>
<body>
<p id="app">
<loading ref='load'></loading>
<button type="button" @click='show'>显示</button>
<button type="button" @click='hide'>隐藏</button>
<button type="button" @click='changeColor'>变色</button>
</p>
</body>
<script type="text/javascript">
let loading = {
data() {
return {
flag: true
}
},
template: '<p v-show="flag">loading...</p>',
methods: {
hide() {
this.flag = false
},
show() {
this.flag = true
}
}
}
let vm = new Vue({
el: '#app',
components: {
loading
},
methods: {
// 在组件上的ref获取组件实例
// 标签的ref 获得标签的dom
// 使用refs 获取组件实例,然后调用组件的方法即可
hide() {
this.$refs.load.hide()
},
show() {
this.$refs.load.show()
},
changeColor() {
// 获取dom实例 操作样式
this.$refs.load.$el.style.background = 'red'
}
}
})
</script>
</html>上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
立即学习“前端免费学习笔记(深入)”;
以上就是在vue中使用ref 让父组件调用子组件的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号