父级组件上的三个按钮可以
调用子组件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中文网其它相关文章!
![php中文网最新课程二维码](/static/images/article_wechat2021.jpg?1111)
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论