zoukankan      html  css  js  c++  java
  • vue.js组件之间通讯--父组件调用子组件的一些方法,子组件暴露一些方法,让父组件调用

    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <div id="app"></div>
    </body>
    <script src="node_modules/vue/dist/vue.js"></script>
    <script>
    //父亲调用子组件的方法,子组件暴露一些方法让父组件调用
    // ref如果写在dom上,表示获取dom,如果写在组件上,表示当前组件的实例
    let vm=new Vue({
    el:"#app",
    template:'<child ref="c"></child>',
    //一定要放在mounted下面,因为mounted方法表示数据和视图渲染完成,
    mounted(){
    //当前的ref指向的是child组件的实例,通过实例调用下面的fn方法
    this.$refs.c.fn();
    },
    components:{
    child:{
    template:'<div>child</div>',
    methods:{
    fn(){
    alert("子子")
    }
    }
    }
    }
    })
    </script>
    </html>
     
  • 相关阅读:
    THUWC2020 游记
    USACO14MAR The Lazy Cow(Gold)
    luogu P3768 简单的数学题
    2017/9/22模拟赛
    2017/9/20模拟赛
    2017/9/15模拟赛
    刷屏代码·稳 from林凯
    2017/9/13模拟赛
    【9018:1368】八数码
    2017/9/10模拟赛
  • 原文地址:https://www.cnblogs.com/null11/p/7479189.html
Copyright © 2011-2022 走看看