Vue组件之间的传参

Vue组件之间的传参方式有三种

  第一:父传子

  第二:子传父

  第三:兄弟之间相传

第一,父传子,顾名思义,外层组件传参给嵌套内部的组件

  在父代中嵌套的标签

  <son v-bind:toSon="fatherData"></son> //发送数据给子代  在子代中接收数据  需要添加一个属性叫做prpos
   props:["toSon"],
  
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <father></father>
    </div>
    <script type="text/javascript">
    Vue.component("father",{
        data:function(){
            return {
                fatherData:"我是你爸爸",
            }
        },
        template:`
            <div>
                <h1>我是父代</h1>
                <input type="text" v-model="fatherData">
                <son v-bind:toSon="fatherData"></son>
            </div>
        `
    })
    Vue.component("son",{
        props:["toSon"],
        template:`<div>
                <h1>我是子代</h1>
                <span>这是我老爸说的话{{ toSon }}</span>
        </div>`
    })
    new Vue({
        el:"#app",
    })

    </script>
</body>
</html>

第二,子传父,

  子代传给父代需要使用方法,具体看代码注释

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <father></father>
    </div>
    <script type="text/javascript">
    Vue.component("father",{
        data:function(){
            return {
                fatherData:"我是你爸爸",
                msg:"",
            }
        },
        methods:{
            resSonData:function(data){  //父代用来接收子代传送的数据
                this.msg = data;
            }
        },
        template:`
            <div>
                <h1>我是父代</h1>
                <span>这是我儿子说的话{{ msg }}</span>
                <son v-on:toFatherSonData="resSonData"></son>  //要在子代上面绑定方法
                //这个v-on后面的参数是一个事件类型名称,=号后面的是方法名
            </div>
        `
    })
    Vue.component("son",{
        data:function(){
            return {
                sonData:"我是你儿子",
            }
        },
        methods:{
            toFather(){ //子代向父代发送数据的方法
                this.$emit("toFatherSonData",this.sonData);
                    // 第一个参数,这是我们自定义的事件类型名,不是方法
            }
        },
        props:["toSon"],
        template:`<div>
                <h1>我是子代</h1>
                <input type="text" v-model="sonData" @input="toFather">
        </div>`
    })
    new Vue({
        el:"#app",
    })
    </script>
</body>
</html>

学完以上两种传参的方式,现在我们来用$refs和$parent两个方法来试试

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件之间的传值-$refs&$parent</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <father></father>
    </div>
    <script type="text/javascript">
    Vue.component("father",{
        data:function(){
            return {
                fatherData:"我是父代组件中内容",
                msg:"",
            }
        },
        methods:{
            chickSon:function(){
                console.log(this.$refs.mySon.sonData);

            }
        },
        template:`
            <div>
                <h1>我是父代</h1>
                <button @click="chickSon">查看子代中内容</button>

                <son  ref="mySon"></son>  

            </div>
        `
    })
    Vue.component("son",{
        data:function(){
            return {
                sonData:"我是子代组件中内容",
            }
        },
        methods:{
            chickFather:function(){
                console.log(this.$parent.fatherData)
            }
        },
        template:`<div>
                <h1>我是子代</h1>
                <button @click="chickFather">查看父代中的内容</button>

        </div>`
    })
    new Vue({
        el:"#app",
    })

    </script>
</body>
</html>

第三,兄弟组件之间的参数传递

注意:兄弟组件传参必须依靠一个vue实例来进行,

关键方法:

$emit("参数名",callblack)    用来发送数据的方法

$on("参数名",callback)  用来接收数据的方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>兄弟组件之间传递参数</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">

        <bigbrother></bigbrother>
        <hr>
        <youngerbrother></youngerbrother>
    </div>
    <script type="text/javascript">
    // 兄弟组件之间传值必须依靠外界
    // 依靠新的一个vue的实例
    var vm = new Vue();
    Vue.component("bigbrother",{
        data:function(){
            return {
                bigBrotherData:"这是哥哥说的话",

            }
        },
        methods:{ //这是发送数据的方法,定义在methods里面
            tellYoungerBrother:function(){
                console.log(this)
                vm.$emit(‘toYoungerBrother‘,this.bigBrotherData);//必须借用Vue的实例来完成数据的传递    

            }
        },
        created:function(){ //这是接收兄弟发来的信息,需要用一个方法来接收
                //关键方法,$on
                vm.$on(‘toBigBrother‘,function(data){
                    console.log(data)

                })
        },
        template:`
            <div>
                <h1>我是哥哥</h1>{{ bigBrotherData }}
                <button @click="tellYoungerBrother">发送给弟弟</button>
                <h2>这是弟弟说的话:</h2>
            </div>
        `
    })
    Vue.component("youngerbrother",{
        data:function(){
            return {
                youngerBrotherData:"这是弟弟说的话"
            }
        },
        methods:{
            tellBigBrother:function(){
                vm.$emit("toBigBrother",this.youngerBrotherData);
            }
        },
        created : function(){
            vm.$on("toYoungerBrother",function(data){
                console.log(data);
            })
        },
        template:`<div>
                <h1>我是弟弟</h1>
                <button @click="tellBigBrother">发送给哥哥</button>
                <h2>这是哥哥说的话:{{  }}</h2>
        </div>`
    })
    new Vue({
            el : "#app",
                data : {
                    msg : "hello"
                }
            })

    </script>
</body>
</html>

原文地址:https://www.cnblogs.com/Godfather-twq/p/11815384.html

时间: 2024-08-03 18:58:03

Vue组件之间的传参的相关文章

子组件以及父子组件之间的传参

子组件 栗子: 需要注意的点都在代码中注释标出来了 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title></title> <style> .wrap { width: calc(200px * 4 + 80px); margin: 0 auto; user-select: none; } .box

vue中组件间的传参

1.父传子 父组件准备一个数据,通过自定义属性给子组件赋值,进行传递 在子组件中通过 props 属性来接收参数 <body> <div id="app"> <son passdata="msg"></son> </div> </body> <script> Vue.component('son', { template: '<div>父组件的数据为:{{ passdat

vue.js 1中父组件跳到子组件中并传参让子组件显示不同的内容

父组件中的点击跳转: <ul class="insurance-entry clearfloat"> <li v-link="{name:'productlist',params:{id:1}}">1</li> <li v-link="{name:'productlist',params:{id:2}}">2</li> <li v-link="{name:'product

【vue组件之间互相传值:父传子,子传父】

今看到一篇很不错的vue组件传值文章,便于理解,遂做笔记- 一般页面的视图App.vue应为这样 一.父组件向子组件传值 1.创建子组件,在src/components/文件夹下新建一个Child.vue2.Child.vue的中创建props,然后创建一个名为message的属性 3.在App.vue中注册Child组件,并在template中加入child标签,标签中添加message属性并赋值 4.保存修改的文件,查看浏览器 5.我们依然可以对message的值进行v-bind动态绑定 此

vue组件之间的数据传递

Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. 首先用 vue-cli 创建一个项目,其中 App.vue 是父组件,components 文件夹下都是子组件. 一.父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据. 子组件部分: 这是 header.vue 的 HTML 部分,logo 是在 data 中定义的变量. 如果需要从父组件获取 logo 的值,就需要使用 props: ['lo

vue组件之间的通信

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

通俗易懂了解Vue组件之间的通信方式

1.前言 Vue框架倡导组件化开发,力求将一个大的项目拆分成若干个小的组件,就如同我们小时玩堆积木一样,一个大房子是由若干个小积木组成.组件化开发最大问题就是组件之间数据能够流通,即组件之间能够通信.而组件间通信无非就分为三种情况:外层的大组件向内部的小组件通信,内部的小组件向外部的大组件通信,平级之间组件通信.说的官方一点就是: 父组件与子组件通信 子组件与父组件通信 非父子组件通信 针对这三种情况,下面将一一展开介绍. 2.父组件 - -> 子组件 父组件向子组件传递数据通过props.通俗

Vue配置路由和传参方式及路由守卫!

安装路由 npm i vue-router -S 引入路由 import VueRouter form VueRouter 注入路由模块 Vue.use(VueRouter) 定义路由匹配规则 let routes = [ {...}, {...} ] 上列匹配规则中 对象有如下属性 path : 路由路径 component : 所加载的组件 name : 别名 redirect : 重定向 children : 子级路由 创建路由实例 let router = new VueRouter({

Vue组件之间的传值方法

在vue当中有两种组件之间的传值方法,分别是 * 父子组件* 之间的传值和* 非父子组件 *之间的传值方法 父子组件之间的传值方法 父子组件之间的传值分为两种 * 父组件给子组件传值 子组件给父组件之间的传值 父组件给子组件传值方法 // 父 <div id = "app"> <my-content></my-content> </div> // 子 <template id="content"> <