时间:2021-07-01 10:21:17 帮助过:8人阅读
父级组件上的三个按钮可以
调用子组件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>上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
详解如何实现vuex(详细教程)
在vue中如何实现微信分享朋友圈,发送朋友
vue.js如何构建大型单页应用
以上就是在vue中使用ref 让父组件调用子组件的详细内容,更多请关注Gxl网其它相关文章!