vue组件父子间通信02

三、组件间通信($parent $refs)

父组件要想获取子组件的数据:
①在调用子组件的时候,指定ref属性
<child-component ref="mySon"></child-component>

②根据指定的引用的名字 找到子组件的实例对象
this.$refs.mySon

子组件要想获取父组件的数据:
①直接读取
this.$parent

:::通过this.$refs拿到子组件的数据

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>组件间通信-01</title>
    <script src="js/vue.js"></script>
 </head>
 <body>
  <div id="container">
        <p>{{msg}}</p>
        <dahua></dahua>
    </div>
    <script>
    //vue提供的ref
        Vue.component("dahua",{
            data:function(){
                return{
                    mySonName:""
                }
            },
            methods:{
            //通过$refs拿到指定的所引用的对应的组件的实例对象
                getSonName:function(){
                    this.mySonName = this.$refs.mySon.name;
                }
            },
            template:`
                <div>
                    <h1>这是父组件</h1>
                    <button @click = "getSonName">获取子组件数据</button>
                    <span>{{mySonName}}</span>
                    <hr>
                    <xiaohua ref="mySon"></xiaohua>
                </div>
            `
        })
//    创建子组件
        Vue.component("xiaohua",{
            data:function(){
                return{
                    name:"小花"
                }
            },
            template:`
                    <h1>这是子组件</h1>
            `
        })
        new Vue({
            el:"#container",
            data:{
                msg:"Hello VueJs"
            }
        })
    </script>
 </body>
</html>

子组件通过$parent获取父组件的数据

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>组件间通信-02</title>
    <script src="js/vue.js"></script>
 </head>
 <body>
  <div id="container">
        <p>{{msg}}</p>
        <dahua></dahua>
    </div>
    <script>
        //创建子组件
        Vue.component("dahua",{
            data:function(){
                return{
                    myName:"大花"
                }
            },
            template:`
                <div>
                    <h1>这是父组件</h1>
                    <hr>
                    <xiaohua></xiaohua>
                </div>
            `
        })
        //创建子组件
        Vue.component("xiaohua",{
            data:function(){
                return{
                    msg:""
                }
            },
            template:`
                <div>
                        <h1>这是子组件</h1>
                        <p>{{msg}}</p>
                </div>
            `,
            created:function(){
                //在子组件创建完成时获取父组件的数据
                //保存在msg中在p标签中显示
                    this.msg = this.$parent.myName;
            }
        })
        new Vue({
            el:"#container",
            data:{
                msg:"Hello VueJs"
            }
        })
    </script>
 </body>
</html>
时间: 2024-08-30 06:24:07

vue组件父子间通信02的相关文章

vue组件父子间通信之综合练习--假的聊天室

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>组件父子间通信之综合练习</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>

Vue组件------父子间通信

子组件传递数据 用户已经登录 父组件接收数据 并显示列表,未登录不显示列表 /* 有两个组件,分别是main-component,header-component.main-component是由header-component和一个列表(有5条数据 [100,200,300,400,500]),header-component是由一个h1的标签:'这是页头',有一个数据isUserLogin:true 在渲染main-component时候,读取header-component在挂载完毕之后通

vue组件之间的通信

vue组件间的通信有父--->子.子--->父.非父子之间的通信 虽然我们稍微复杂的项目都用vuex来管理了,但是还是想写一篇关于有父--->子.子--->父.非父子 之间通信的文章.下面通过代码来讲述 父--->子组件间的通信 父级页面: <template> <div id="app"> <Header :parentMsg='parentMsg' > </Header> </div> <

vue组件中的通信

一.组件间的关系 1.父子关系 2.兄弟关系 3.隔代关系 二.组件间的通信方式 1.props 2.$emit/$on 3.VUEX 4.$parent/$children 5.$attrs/$listeners 6.provide/inject 三.通信方式举例 新建了一个过程,采用webpack来管理项目.  方法一:props / $emit 1.props---父组件向子组件传值 子组件:sub1.vue 父组件:app.vue 父组件通过props向下传递给子组件.注:组件中的数据共

父子间通信四 ($dispatch 和 $broadcast用法)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js测试父子之间通信</title> <script type="text/javascript" src="lib/boot.js"></script> <style> .box{

Vue组件~父子组件之间的通信(传值)

1.父组件向子组件传参 父组件中通过v-bind对要传参数的绑定:(例如) :dataSourceName:是在子组件中要用到的值 "ctpSaveEchartSetting.dataSourceId":是在父组件中要传的值 子组件中的接收通过props(数据值单项绑定的)进行接收:(例如) 注:在props中有的变量就不能再data中再定义,或通过监测.计算属性直接去改变,因为js中的对象和数组是引用对象,指向同一个内存空间,如果在子组件中改变props中的值会影响到父组件中的值的状

vue组件父子之间相互通信案例

vue组件父子组件之间传递数据

举个栗子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../Vue.js"></script> <template id="tpl1"> <h3>我是父组件

vue组件之间的通信以及如何在父组件中调用子组件的方法和属性

在Vue中组件实例之间的作用域是孤立的,以为不能直接在子组件上引用父组件的数据,同时父组件也不能直接使用子组件的数据 一.父组件利用props往子组件传输数据 父组件: <div> <child v-bind:my-message="parentMsg"></child>//注意传递参数时要用-代替驼峰命名,HTML不区分大小写 </div> 子组件: Vue.component('child', { // camelCase in Ja